What ShellCheck checks
ShellCheck parses a script and flags constructs that are valid but risky. The most frequent findings concern unquoted variables, which split at spaces and expand globs, the use of backticks instead of $(...), reads of unset variables, and cd without a failure check. It also flags bashisms when the script declares itself as POSIX sh. Each finding has a code, and the tool documentation explains the fix.
ShellCheck is a static tool. It does not run your script, so it cannot see the values that will be present at run time. That is why its warnings are suggestions to think about, not proof of a bug, and why you still need to test.
An example of a finding
Consider a script that copies a file whose name comes from an argument. The unquoted form works for simple names and fails for names with spaces, which is exactly the kind of case a linter is designed to catch.
cp $1 /backup/ShellCheck reports SC2086, which says the variable should be quoted to prevent word splitting and globbing. The corrected line, cp "$1" /backup/, passes the argument as one word. This is the single most common fix in shell code reviews.
Running ShellCheck
ShellCheck is usually installed as a separate package and run as shellcheck script.sh. It prints each warning with its line number and code. Many editors show the same warnings inline. You can silence a single finding with a comment such as # shellcheck disable=SC2086 placed directly above the line, but do that only after you have understood why the warning applies and decided that the code is correct.
The lab machine for this course does not include ShellCheck. The lab uses bash -n, which is part of bash itself, to check syntax. The habits in this unit still apply on any machine where ShellCheck is available.
Checking syntax with bash -n
The -n option tells bash to read a script and check its syntax without running any commands. It catches unbalanced quotes, a missing fi, and a missing done. It is a fast first check, and it is always available.
printf "%s\n" "if [ 1 -eq 1 ]; then" " echo yes" > broken.sh
bash -n broken.sh; echo "status=$?"The check reports a syntax error about unexpected end of file, because the if was never closed, and the status is 2. A script with correct syntax returns status 0 from bash -n, but that does not prove it does the right thing.
Fixing a quoting warning
Use the same mindset as ShellCheck when you write code. Quote every expansion unless you want word splitting, and prefer the $(...) form for command substitution. The next block shows a line that is correct for a name with a space and that a linter would accept.
name="my notes.txt"
touch "$name"
ls -l "$name"The file has one name with a space, which the quotes preserved. If the quotes were missing, touch would create two files.
A review checklist
When you review a script without a linter, check the common items in order. Is every expansion quoted? Does every cd and rm have a failure check? Are temporary files created with mktemp and removed in a trap? Does the script start with strict mode? Each question maps to a ShellCheck category, and asking them by hand catches much of what the tool would report.
Hands-on: find the bug by hand
Run the syntax check on a deliberately broken script, and read the error. The lab for this unit asks you to write one broken script, record its syntax error, and then write a corrected script that quotes its variable.
printf "%s\n" "echo \"unterminated" > bad.sh
bash -n bad.sh; echo "status=$?"The check reports an unexpected end of file, because the double quote is never closed, and the status is 2.