shellcheck: A Second Pair of Eyes for Shell Scripts

The script ran. That does not mean the next filename with a space will. shellcheck reads the script and says so before the space shows up.

shellcheck is a linter for sh, bash, and ksh. It does not run the script. It reads it and reports the bugs that only appear when a variable is empty, or a filename has a space, or you used a bashism in a script marked #!/bin/sh. Checked on 0.11.0: an unquoted $1 produced SC2086, and a quoted script exited 0.

install

macOS
# macOS
brew install shellcheck
Debian/Ubuntu
# in the default repos
sudo apt install shellcheck
Fedora
# package name is ShellCheck, capital S and C
sudo dnf install ShellCheck
Arch
# in the default repos
sudo pacman -S shellcheck

examples

# Lint a script. Exit 0 means no findings
shellcheck script.sh
# gcc-style output, one finding per line. An unquoted $1 reports SC2086
shellcheck -f gcc script.sh

pro tip

The shebang is the dialect. #!/bin/sh gets POSIX rules. #!/bin/bash is allowed to use arrays. If the shebang is wrong, the warnings are wrong. Fix that line before you silence a code.

honest limits

shellcheck does not execute the script and does not know what your files look like. A clean report is not a test. It also will not catch a logic bug that is valid shell. Do not add a directive to hide SC2086. Quote the expansion.