ShellCheck
ShellCheck is a static analyser for shell scripts. It knows hundreds of Bash pitfalls: unquoted variables, cd
without error handling, local x=$(…), globs that match nothing, [ $a == $b ] in sh scripts. Most of the traps
in this section are things ShellCheck reports before the script ever runs. If you adopt one tool from this course,
make it this one, and run it in CI on every shell script in the repository.
A first run
cat > deploy.sh <<'SCRIPT'
#!/usr/bin/env bash
set -euo pipefail
release=$1
target=/srv/app/releases/$release
cd $target
for f in $(ls *.tar.gz); do
tar -xzf $f
done
local_version=$(cat VERSION)
echo "deployed $release" | tee -a $LOGFILE
SCRIPT
shellcheck deploy.sh; echo "-> status $?"
In deploy.sh line 5:
cd $target
^-----^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
cd "$target"
In deploy.sh line 6:
for f in $(ls *.tar.gz); do
^------------^ SC2045 (error): Iterating over ls output is fragile. Use globs.
^-- SC2035 (info): Use ./*glob* or -- *glob* so names with dashes won't become options.
In deploy.sh line 7:
tar -xzf $f
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
tar -xzf "$f"
In deploy.sh line 9:
local_version=$(cat VERSION)
^-----------^ SC2034 (warning): local_version appears unused. Verify use (or export if used externally).
In deploy.sh line 10:
echo "deployed $release" | tee -a $LOGFILE
^------^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
echo "deployed $release" | tee -a "$LOGFILE"
For more information:
https://www.shellcheck.net/wiki/SC2045 -- Iterating over ls output is fragi...
https://www.shellcheck.net/wiki/SC2034 -- local_version appears unused. Ver...
https://www.shellcheck.net/wiki/SC2035 -- Use ./*glob* or -- *glob* so name...
-> status 1
Every finding has a code (SC2086), a severity and a link to a wiki page that explains the problem and the fix; the
codes are worth searching for. Here they say:
- SC2086 (four times), the most common finding in real scripts: an unquoted variable is split on spaces and
expanded as a glob. Quote it:
"$target","$f". It is ratedinfobecause it is only a bug when the value can contain spaces or*, but a release name or a path from a user eventually will. - SC2045 (error): iterating over
lsoutput breaks on file names with spaces. Use the glob directly:for f in *.tar.gz. SC2035 adds that a file named-rf.tar.gzwould be read as options. - SC2034 (warning):
local_versionis assigned and never used, often a sign of a typo or of a half-finished edit.
What it did not report is just as instructive. cd $target without || exit would normally be SC2164, but
ShellCheck sees set -e and stays quiet. And $LOGFILE is never set: with set -u the script dies on the last line,
which ShellCheck cannot know because the variable might come from the environment. Static analysis complements
testing; it does not replace it.
The fixed script
cat > deploy.sh <<'SCRIPT'
#!/usr/bin/env bash
set -euo pipefail
release=${1:?usage: deploy.sh RELEASE}
target=/srv/app/releases/$release
cd "$target" || exit
for f in *.tar.gz; do
tar -xzf "$f"
done
echo "deployed $release" | tee -a "${LOGFILE:-/dev/null}"
SCRIPT
shellcheck deploy.sh && echo "no findings"no findings
Note target=/srv/app/releases/$release without quotes is fine: assignments are not word-split, and ShellCheck knows
it. Quoting it anyway costs nothing.
Output formats for editors and CI
The default format is for humans. CI systems and editors want one finding per line:
printf '#!/bin/sh\nname=$1\nif [ $name == "admin" ]; then echo "hi $name"; fi\n' > greet.sh
shellcheck -f gcc greet.sh; echo "-> status $?"
echo "--- only warnings and errors:"
shellcheck -S warning -f gcc greet.sh; echo "-> status $?"greet.sh:3:6: note: Double quote to prevent globbing and word splitting. [SC2086]
greet.sh:3:12: warning: In POSIX sh, == in place of = is undefined. [SC3014]
-> status 1
--- only warnings and errors:
greet.sh:3:12: warning: In POSIX sh, == in place of = is undefined. [SC3014]
-> status 1
-f gccgivesfile:line:col: level: message [SCxxxx], which most editors and CI annotators understand.-f json1,-f checkstyleand-f diff(a patch for auto-fixable findings) also exist.-S warninghidesinfoandstylefindings (here the SC2086 note), a sensible threshold for a first CI gate on an old code base.- The shebang matters: this file says
#!/bin/sh, so ShellCheck checks it as POSIXshand warns about==(a Bash-only operator inside[ ], SC3014). Use-s bashto override the dialect for files without a shebang. - Exit status is 1 when anything at or above the threshold was found, so
shellcheckfails a CI step directly.
Silencing a finding, deliberately
Sometimes the code is right and ShellCheck cannot know it. Disable the specific code on the specific line, and say why:
cat > build.sh <<'SCRIPT'
#!/usr/bin/env bash
set -euo pipefail
CFLAGS="-O2 -Wall"
# Word splitting is intended: CFLAGS holds several flags.
# shellcheck disable=SC2086
echo gcc $CFLAGS -o app main.c
# shellcheck source=lib.sh
source ./lib.sh
greet
SCRIPT
printf 'greet() { echo "hello from lib"; }\n' > lib.sh
shellcheck build.sh || echo "-> status $?"
shellcheck -x build.sh && echo "build.sh with -x (follows sourced files): clean"
In build.sh line 8:
source ./lib.sh
^------^ SC1091 (info): Not following: lib.sh was not specified as input (see shellcheck -x).
For more information:
https://www.shellcheck.net/wiki/SC1091 -- Not following: lib.sh was not spe...
-> status 1
build.sh with -x (follows sourced files): clean
- Without
-x, ShellCheck refuses to openlib.sh(SC1091,info), so it cannot tell whethergreetexists. - A
# shellcheck disable=SCxxxxcomment applies to the next command; put it at the top of the file (after the shebang) to cover the whole file. Better still, fix the code: an array (cflags=(-O2 -Wall), then"${cflags[@]}") needs no exception. -xlets ShellCheck followsourcelines and check the sourced file's functions and variables too;# shellcheck source=pathtells it where a dynamically named file lives.- A
.shellcheckrcin the repository holds project-wide settings, for exampleexternal-sources=trueordisable=SC1091.
ShellCheck in CI
# .github/workflows/lint.yml (excerpt)
jobs:
shellcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: ShellCheck every tracked shell script
run: git ls-files -z '*.sh' | xargs -0 --no-run-if-empty shellcheck -x -S warning -f gccShellCheck is preinstalled on GitHub's Ubuntu runners and packaged in every major distribution; shfmt (a
formatter) and the pre-commit hook that runs both are in the Toolkit.
Test yourself
What is ShellCheck's most frequent finding, SC2086, about?
- Missing shebang
- An unquoted variable that will be word-split and glob-expanded
- Use of
set -e - Tabs instead of spaces
How should you suppress a ShellCheck finding that is a false positive?
- Delete the line
- Add
# shellcheck disable=SCxxxxabove that command, with a comment explaining why - Run ShellCheck with
-S erroreverywhere - Rename the file to .txt
Why did ShellCheck warn about == in greet.sh?
==is never valid in shell- The shebang is
#!/bin/sh, and in POSIXshthe[ ]test only has= $namewas unquoted- ShellCheck dislikes the word admin