ShellCheck

ShellCheckarrow-up-right is a shell script static analysis tool.

1. Overview

ShellCheck is a GPLv3 tool that gives warnings and suggestions for bash/sh shell scripts:

The goals of ShellCheck are

  • To point out and clarify typical beginner's syntax issues that cause a shell to give cryptic error messages.

  • To point out and clarify typical intermediate level semantic problems that cause a shell to behave strangely and counter-intuitively.

  • To point out subtle caveats, corner cases and pitfalls that may cause an advanced user's otherwise working script to fail under future circumstances.

See the gallery of bad code for examples of what ShellCheck can help you identify!

2. How to use

There are a number of ways to use ShellCheck!

2.1. On the WEB

Paste a shell script on https://www.shellcheck.netarrow-up-right for instant feedback.

2.2. From your terminal

Run shellcheck <your-script> in your terminal for instant output.

2.3. In your Editor

You can see ShellCheck suggestions directly in a variety of editors.

2.4. In your build or test suites

While ShellCheck is mostly intended for interactive use, it can easily be added to builds or test suites.

It makes canonical use of exit codes, so you can just add a shellcheck command as part of the process.

For example, in a Makefile:

or in a Travis CI .travis.yml file:

Services and platforms that have ShellCheck pre-installed and ready to use:

Most other services, including GitLabarrow-up-right, let you install ShellCheck yourself.

It's a good idea to manually install a specific ShellCheck version regardless. This avoids any surprise build breaks when a new version with new warnings is published.

For customized filtering or reporting, ShellCheck can output simple JSON, CheckStyle compatible XML, GCC compatible warnings as well as human readable text (with or without ANSI colors). See the Integrationarrow-up-right wiki page for more documentation.

3. Installing

3.1. Using Package Manager

The easiest way to install ShellCheck locally is through your package manager.

On macOS with Homebrew:

On Debian based distros:

On Arch Linux based distros:

or get the dependency free shellcheck-binarrow-up-right from the AUR.

On EPEL based distros:

On Fedora based distros:

On FreeBSD:

On OpenBSD:

On Windows

From conda-forgearrow-up-right:

From Snap Store:

From Docker Hub:

Using the nix package managerarrow-up-right:

3.2. pre-commit

To run ShellCheck via pre-commitarrow-up-right, add the hook to your .pre-commit-config.yaml:

3.3. Travis CI

Travis CI has now integrated ShellCheck by default, so you don't need to manually install it.

4. Ignoring issues

https://github.com/koalaman/shellcheck/wiki/Ignorearrow-up-right

Issues can be ignored via environmental variable, command line, individually or globally within a file:

4.1. Ignoring one specific instance in a file

Use a directivearrow-up-right to disable a certain instance:

You can pass multiple errors to directive:

4.2. Ignoring all instances in a file

Add a directive at the top of the file:

Note that the directive must be on the first line after the shebang with versions before 0.4.6. As of 0.4.6 comments and whitespace are allowed before file-wide directives.

4.3. Ignoring all errors in a file

Add a directive at the top of the file:

Note that the directive must be on the first non-commented/non-whitespace line after the shebang with versions after 0.4.6.

4.4. Ignoring errors in one specific run

Use a -e flag to disable a specific error when running shellcheck:

4.5. Ignoring one or more types of errors forever

You can create a file .shellcheckrc in your home directory (or your project's base directory), and add disable directives to it:

So what kind of things does ShellCheck look for? Here is an incomplete list of detected issues.

5.1. Quoting

ShellCheck can recognize several types of incorrect quoting:

5.2. Conditionals

ShellCheck can recognize many types of incorrect test statements.

5.3. Frequently misused commands

ShellCheck can recognize instances where commands are used incorrectly:

5.4. Common beginner's mistakes

ShellCheck recognizes many common beginner's syntax errors:

5.5. Style

ShellCheck can make suggestions to improve style:

5.6. Data and typing errors

ShellCheck can recognize issues related to data and typing:

5.7. Robustness

ShellCheck can make suggestions for improving the robustness of a script:

5.8. Portability

ShellCheck will warn when using features not supported by the shebang. For example, if you set the shebang to #!/bin/sh, ShellCheck will warn about portability issues similar to checkbashisms:

5.9. Miscellaneous

ShellCheck recognizes a menagerie of other issues:

6. Other Resources

Last updated