ShellCheck analyzes shell scripts and reports common errors, unsafe patterns, portability problems, and style issues.
It is included in this setup to improve the reliability of Bash and shell scripts before they are committed or executed.
ShellCheck is installed through Homebrew:
brew install shellcheckIt is part of the curated Homebrew environment; see Homebrew setup to install everything at once.
Check that ShellCheck is available:
shellcheck --versionRun ShellCheck against a shell script with:
shellcheck path/to/script.shSeveral scripts can be analyzed in a single command:
shellcheck script-one.sh script-two.shTo analyze all shell scripts tracked by Git:
git ls-files '*.sh' -z | xargs -0 -r shellcheckShellCheck uses the script shebang to determine which shell dialect should be analyzed.
For example:
#!/usr/bin/env bashScripts should declare an explicit and accurate shebang so that ShellCheck can apply the correct rules.
A dialect can also be selected manually:
shellcheck --shell=bash path/to/script.shShellCheck findings include a code such as SC2086 or SC2155.
These codes make it possible to:
- understand the reported problem;
- consult the corresponding rule documentation;
- suppress one specific rule when there is a justified exception.
Warnings should normally be fixed instead of ignored.
A specific finding can be disabled for the following line:
# shellcheck disable=SC2086
command $valueExclusions should remain narrow and include enough context to explain why the warning is intentionally ignored.
Project-wide exclusions should be avoided unless they are clearly justified.
ShellCheck should eventually be integrated into pre-commit when shell scripts are present in the repository.
Homebrew will continue to manage the executable, while pre-commit will define when and against which files it runs.
Until then, ShellCheck can be executed manually:
git ls-files '*.sh' -z | xargs -0 -r shellcheckRemove ShellCheck with:
brew uninstall shellcheckThen remove its entry from profiles/full/Brewfile.
Any related pre-commit hook must also be removed separately.
