$_ CLI In Depth
Learning progress 0%
lesson robustness traps debugging

Linting Scripts with ShellCheck

4 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:11

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.

BASH1 lines
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.

Note

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.

BASH2 lines
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.

BASH3 lines
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.

BASH2 lines
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.

Terminal lab · 2 steps · real Linux in your browser

Read a syntax error and fix quoting

Create a broken script, save bash -n's complaint, then write a quoted version that handles a name with a space.

01
Record a syntax error
Create broken.sh in /workspace with two lines: if [ 1 -eq 1 ]; then and echo yes. The if is never closed. Run bash -n on the file and save its error output to syntax.txt: ``bash printf '%s\n' 'if [ 1 -eq 1 ]; then' ' echo yes' > broken.sh bash -n broken.sh 2> syntax.txt `` Open syntax.txt and read the message.
Show hint for step 1
Redirect standard error with 2> syntax.txt. The message should name the line and say syntax error.
02
Write a quoted script
Create fixed.sh in /workspace. It takes its first argument into a variable named name and prints it with printf, quoting the variable: ``bash cat > fixed.sh <<'EOF' #!/bin/bash name=$1 printf '%s\n' "$name" EOF `` Run it with the argument "my notes.txt". It must print one line with the whole name.
Show hint for step 2
Quote the variable in the printf call. Without quotes, the name is split at the space and printf prints two lines.
Check yourself Question 1 of 4
Concept Check Syntax check without running recall

Which command, available on the lab machine, checks a script's syntax without running any of its commands?

Recap & Key Takeaways

ShellCheck is a static linter that reports risky shell constructs by code, and its most common finding is an unquoted variable such as SC2086.
A linter cannot see run-time values, so its warnings are reasons to check the code, not proof of a bug.
bash -n checks syntax without running the script and is always available on the lab machine.
A manual review checklist covers quoting, cd and rm failure checks, temporary files, and strict mode.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue