shellcheck

Static analysis tool for shell scripts

TLDR

Check a shell script
$ shellcheck [script.sh]
Check multiple scripts
$ shellcheck [script1.sh] [script2.sh]
Check with specific shell dialect
$ shellcheck --shell=[bash|sh|dash|ksh] [script.sh]
Output in different format (JSON, GCC, etc.)
$ shellcheck --format=[json|gcc|checkstyle] [script.sh]
Exclude specific warnings
$ shellcheck --exclude=[SC2034,SC2086] [script.sh]
Check script from stdin
$ cat [script.sh] | shellcheck -
Enable optional checks
$ shellcheck --enable=all [script.sh]

SYNOPSIS

shellcheck [options] [script...]

DESCRIPTION

shellcheck is a static analysis tool for shell scripts. It detects bugs, syntax issues, and pitfalls in bash/sh/ksh/dash scripts, providing explanations and suggestions for fixes.Each warning has a code (e.g., SC2086) linking to detailed wiki documentation explaining the issue, why it matters, and how to fix it. This makes shellcheck excellent for learning shell scripting best practices.The tool catches common issues like unquoted variables, deprecated syntax, command substitution pitfalls, and potential security vulnerabilities. It distinguishes between different shell dialects and their specific features.Integration is available for most editors (VS Code, Vim, Emacs, Sublime) and CI systems, enabling automated script checking.

PARAMETERS

-s, --shell dialect

Specify shell dialect (sh, bash, dash, ksh)
-f, --format format
Output format (tty, gcc, json, checkstyle, diff, quiet)
-e, --exclude codes
Exclude specific error codes (comma-separated)
-i, --include codes
Include only specific error codes
--enable checks
Enable optional checks (all, require-variable-braces, etc.)
-x, --external-sources
Follow and check sourced files
-a, --check-sourced
Check sourced files for issues
-S, --severity level
Minimum severity (error, warning, info, style)
-V, --version
Display version
-C, --color when
Colorize output (auto, always, never).
--wiki-link-count n
Include wiki links for first n warnings.

CONFIGURATION

.shellcheckrc

Project-level configuration file for default options, disabled warnings, and shell dialect. Also read from ~/.shellcheckrc.

COMMON WARNINGS

SC2086: Double quote to prevent globbing and word splittingSC2034: Variable appears unusedSC2046: Quote to prevent word splittingSC2006: Use $(...) instead of backticksSC2035: Use ./\* so patterns don't expand to optionsSC2164: Use cd ... || exit in case cd fails

INSTALL

sudo apt install shellcheck
sudo pacman -S shellcheck
sudo apk add shellcheck
brew install shellcheck
nix profile install nixpkgs#shellcheck

CAVEATS

ShellCheck may produce false positives when variables are used indirectly or sourced from external files. Use # shellcheck disable=SC#### comments to suppress specific warnings.Some legitimate shell patterns trigger warnings. Review each warning before disabling; often the suggested fix is genuinely better.The --external-sources option requires caution as it can execute arbitrary code when checking scripts that source files.

HISTORY

ShellCheck was created by Vidar Holen and first released around 2012. It is written in Haskell and has become the de facto standard linter for shell scripts, widely integrated into CI pipelines and editor plugins.

SEE ALSO

bash(1), sh(1), lint(1), ksh(1)