Linux and servers · Beginner · 15 min
Find bugs in shell scripts with ShellCheck
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · ShellCheck 0.11.0 · GNU Bash 5.3
You will run ShellCheck 0.11 on a broken deploy script, fix the five bugs it reports, silence one check with a written reason, set project defaults in a .shellcheckrc, and run it before every commit and in CI.
On this page
ShellCheck reads a shell script without running it and reports the mistakes that make scripts fail in odd ways: unquoted variables that split on spaces, a cd that fails and leaves the script working in the wrong directory, an exit code that gets thrown away. Each report has a code, such as SC2086, with a wiki page that explains it.
In this tutorial you write a short deploy script with five real bugs, watch one of them copy the wrong files, then let ShellCheck find all five. You fix them, silence one check on purpose with a reason next to it, set project defaults in a .shellcheckrc, and wire ShellCheck into pre-commit and a CI job so the bugs cannot come back.
Everything happens in ~/shellcheck-demo, a throwaway folder you delete at the end.
Step 1 · Install ShellCheck
ShellCheck is in Ubuntu's universe component, which Ubuntu 26.04 enables by default. Ubuntu 26.04 ships ShellCheck 0.11.0, the current upstream release.
sudo apt-get update
sudo apt-get install -y shellcheck
shellcheck --versionThen make the demo folders. A release to deploy lives in a folder whose name has a space in it, which is common for folders people create by hand:
mkdir -p ~/shellcheck-demo/scripts ~/shellcheck-demo/live "$HOME/shellcheck-demo/releases/build 42"
cd "$HOME/shellcheck-demo/releases/build 42"
echo "hello from build 42" > app.txt
echo "version 42" > VERSION
echo "scratch data" > cache.tmp
lsCheck it worked
shellcheck --version prints version: 0.11.0 along with the licence and website. The last ls lists app.txt, cache.tmp and VERSION.
Step 2 · Write a deploy script and watch it fail
This script copies a release into ~/shellcheck-demo/live, removes temporary files and checks for a VERSION file. It looks reasonable and contains five bugs. Create it exactly as shown:
cd ~/shellcheck-demo/scripts
cat > deploy.sh <<'EOF'
#!/bin/bash
# deploy.sh: copy a release into the live directory
release_dir=$1
target=$HOME/shellcheck-demo/live
export BUILD_ID=$(date +%Y%m%d%H%M%S)
cd $release_dir
cp -r * $target
rm -f $(find $target -name '*.tmp')
grep -q "version" $target/VERSION
if [ $? -ne 0 ]; then
echo "No VERSION file in the release" >&2
exit 1
fi
echo "Deployed build $BUILD_ID to $target"
EOFRun it against the release, then look at what landed in live:
bash deploy.sh "../releases/build 42" || echo "deploy.sh exited with code $?"
ls ../liveHere is what went wrong. $release_dir holds ../releases/build 42. Without quotes, Bash splits it at the space, so cd receives two arguments, prints an error and fails. The script carries on anyway, still in scripts/, and cp -r * copies the script itself into live. The VERSION check then fails because the real release never arrived.
Check it worked
You see an error from cd, something like deploy.sh: line 8: cd: too many arguments, then a grep error about live/VERSION, No VERSION file in the release and deploy.sh exited with code 1. ls ../live shows deploy.sh, not app.txt. On a server this is how a deploy script copies the wrong files into production.
Step 3 · Run ShellCheck and read the report
Point ShellCheck at the script. It exits with code 1 when it has anything to report, so the || part prints a message instead of stopping your shell session:
shellcheck deploy.sh || echo "ShellCheck found problems"Each finding shows the line, a caret under the exact spot, a code, a severity (error, warning, info or style) and a one-line explanation. The ones that matter here:
| Line | Code | What it means |
|---|---|---|
| 6 | SC2155 | export VAR=$(cmd) hides the exit code of cmd: export succeeds even if date fails |
| 8 | SC2164 | cd can fail, and nothing stops the script when it does |
| 8 and others | SC2086 | An unquoted $variable is split at spaces and expanded as a glob |
| 11 | SC2046 | An unquoted $(command) is split in the same way, so file names with spaces break rm |
| 14 | SC2181 | Testing $? on the next line is fragile; test the command itself |
You may also see SC2035 on cp -r *, because a file named -r or --force in the folder would be read as an option.
For a report that is easier to search or feed to an editor, ask for one line per finding:
shellcheck -f gcc deploy.sh || echo "ShellCheck found problems"Check it worked
The report names SC2155 on line 6, SC2164 and SC2086 on line 8, SC2046 on line 11 and SC2181 on line 14. The -f gcc version prints lines like deploy.sh:8:1: warning: Use 'cd ... || exit' or 'cd ... || return' in case cd fails. [SC2164]. Line 8 is the bug you just watched happen.
Step 4 · Look up a code on the wiki
The default report ends with links such as https://www.shellcheck.net/wiki/SC2164. Every code has a page in the same place. Open the one for SC2164 in a browser.
Each page has the same parts: the problematic code, the correct code, a rationale, and an "Exceptions" section that says when it is fine to ignore the check. Read the rationale and the exceptions before you change anything. They tell you whether the fix is the one ShellCheck suggests, or whether your case is one of the exceptions.
If you work on prepaid data or an unreliable connection, you can keep the whole wiki on disk. It is a Git repository, and each page is a Markdown file named after its code:
git clone --depth 1 https://github.com/koalaman/shellcheck.wiki.git ~/shellcheck-wiki
less ~/shellcheck-wiki/SC2164.mdShellCheck itself never needs the network, so it works the same on a laptop with no connection.
Check it worked
You can say in one sentence, from the SC2164 page, why cd without a fallback is dangerous and what the suggested fix is: add || exit (or || return in a function) so the script stops when the directory does not exist.
Step 5 · Fix the five bugs
Rewrite deploy.sh with each fix in place:
cat > deploy.sh <<'EOF'
#!/bin/bash
# deploy.sh: copy a release into the live directory
set -euo pipefail
release_dir=${1:?usage: deploy.sh RELEASE_DIR}
target=${DEPLOY_TARGET:-$HOME/shellcheck-demo/live}
BUILD_ID=$(date +%Y%m%d%H%M%S)
export BUILD_ID
cd "$release_dir" || exit 1
cp -r ./* "$target"/
find "$target" -name '*.tmp' -delete
if ! grep -q "version" "$target/VERSION"; then
echo "No VERSION file in the release" >&2
exit 1
fi
echo "Deployed build $BUILD_ID to $target"
EOF
shellcheck deploy.sh && echo "deploy.sh is clean"What changed, code by code:
- SC2155: the assignment and the
exportare separate lines, so a failingdatestops the script underset -einstead of being masked.BUILD_IDstays exported so any command the script starts later, such as a restart hook, can read it. - SC2164:
cd ... || exit 1stops the script if the folder is missing. - SC2086: every variable is in double quotes, so
build 42stays one argument. - SC2046: the
rm -f $(find ...)line is gone.find -deleteremoves the matches itself, with no word splitting at all. - SC2181:
if ! grep -q ...tests the command directly. - SC2035:
./*instead of*, so a file name that starts with-cannot become an option.
set -euo pipefail and ${1:?...} are extras ShellCheck does not ask for. They make the script stop on errors, unset variables and a missing argument. The strict mode tutorial linked at the end covers them in depth.
The next command deletes the live folder and everything in it, including the stray deploy.sh copy from the failed run. Check the path before you press Enter:
rm -rf ~/shellcheck-demo/live
mkdir ~/shellcheck-demo/live
bash deploy.sh "../releases/build 42"
ls ../liveThen try a release that does not exist. The script should stop at cd and leave live alone:
bash deploy.sh "../releases/no such build" || echo "deploy.sh exited with code $?"
ls ../liveCheck it worked
shellcheck deploy.sh prints nothing, then deploy.sh is clean. The deploy prints Deployed build <timestamp> to /home/<you>/shellcheck-demo/live, and ls ../live shows app.txt and VERSION with no cache.tmp. The second run prints a cd error and deploy.sh exited with code 1, and live still holds the same two files.
Step 6 · Silence a check on purpose, with a reason
Sometimes ShellCheck is wrong about your intent. Add a closing hint to the script that shows the reader a command to type, with $HOME written literally:
cat >> deploy.sh <<'EOF'
echo 'To deploy somewhere else, run: DEPLOY_TARGET="$HOME/other" bash deploy.sh RELEASE_DIR'
EOF
shellcheck deploy.sh || echo "ShellCheck found problems"ShellCheck reports SC2016: expressions do not expand in single quotes. Usually that is a bug. Here it is the point: the text must show $HOME, not your home folder. This is the case for a disable directive. Put it on the line before the command it covers, and give the reason on the same line after a second #, so the next person to read the script does not have to guess:
cat > deploy.sh <<'EOF'
#!/bin/bash
# deploy.sh: copy a release into the live directory
set -euo pipefail
release_dir=${1:?usage: deploy.sh RELEASE_DIR}
target=${DEPLOY_TARGET:-$HOME/shellcheck-demo/live}
BUILD_ID=$(date +%Y%m%d%H%M%S)
export BUILD_ID
cd "$release_dir" || exit 1
cp -r ./* "$target"/
find "$target" -name '*.tmp' -delete
if ! grep -q "version" "$target/VERSION"; then
echo "No VERSION file in the release" >&2
exit 1
fi
echo "Deployed build $BUILD_ID to $target"
# shellcheck disable=SC2016 # the reader types this command, so $HOME must stay literal
echo 'To deploy somewhere else, run: DEPLOY_TARGET="$HOME/other" bash deploy.sh RELEASE_DIR'
EOF
shellcheck deploy.sh && echo "deploy.sh is clean"Three rules keep directives honest:
- Disable one code at a time, on the one command it applies to. A directive straight after the shebang applies to the whole file, which hides future bugs too.
- Always write the reason. "Intentional" is not a reason; say what is intended.
- Never disable SC2086 or SC2164 to make a report go away. Fix the quoting or the
cdinstead.
Check it worked
The first shellcheck run reports SC2016 on the new echo line. After the rewrite, shellcheck deploy.sh prints deploy.sh is clean, and the directive line in the file says why the check is off.
Step 7 · Set project defaults and the shell dialect
ShellCheck decides which shell rules to apply from the shebang. A file that is only ever sourced by other scripts has no shebang, so ShellCheck cannot tell. Create a small helper file like that:
cat > common.sh <<'EOF'
# common.sh: helpers sourced by other scripts; never run on its own
log() {
local message=$1
echo "$(date +%H:%M:%S) $message" >&2
}
EOF
shellcheck common.sh || echo "ShellCheck found problems"ShellCheck reports SC2148: it does not know the target shell. Instead of adding a directive to every helper, set a project default. ShellCheck looks for a .shellcheckrc in the script's folder, then each parent folder, then your home folder, and uses the first one it finds. Put one at the top of the project:
cat > ~/shellcheck-demo/.shellcheckrc <<'EOF'
# Files with no shebang and no shell directive are Bash.
shell=bash
EOF
shellcheck common.sh && echo "common.sh is clean"Now suppose a nightly job that runs under /bin/sh also sources common.sh. On Ubuntu /bin/sh is dash, which does not have every Bash feature. A shell directive at the top of the file overrides the project default for that file:
cat > common.sh <<'EOF'
# shellcheck shell=sh
# common.sh: helpers sourced by Bash scripts and by /bin/sh jobs
log() {
local message=$1
echo "$(date +%H:%M:%S) $message" >&2
}
EOF
shellcheck common.sh || echo "ShellCheck found problems"SC3043 says local is not defined in POSIX sh. Rewrite the function without it, so it works under both shells:
cat > common.sh <<'EOF'
# shellcheck shell=sh
# common.sh: helpers sourced by Bash scripts and by /bin/sh jobs
log() {
printf '%s %s\n' "$(date +%H:%M:%S)" "$1" >&2
}
EOF
shellcheck common.sh && echo "common.sh is clean"The same file can turn on extra checks with enable= lines; shellcheck --list-optional shows them, such as require-variable-braces. Commit .shellcheckrc with the scripts so everyone, and CI, gets the same results.
Check it worked
The first run reports SC2148. With .shellcheckrc in place it prints common.sh is clean. With # shellcheck shell=sh it reports SC3043 on the local line, and the printf version prints common.sh is clean again.
Step 8 · Run ShellCheck before every commit and in CI
A linter only helps if it runs every time. Check the whole scripts folder at once from the project root:
cd ~/shellcheck-demo
shellcheck -f gcc scripts/*.sh && echo "all scripts pass"In a Git repository, add the ShellCheck hook to .pre-commit-config.yaml. The shellcheck-py project packages the same ShellCheck binary for pre-commit, and its tag v0.11.0.1 is ShellCheck 0.11.0:
repos:
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.11.0.1
hooks:
- id: shellcheckInstall the hook once, then run it over every file the first time. Setting up pre-commit itself is covered in the pre-commit tutorial linked below.
pre-commit install
pre-commit run shellcheck --all-filesFrom then on, git commit runs ShellCheck on the shell scripts you changed and refuses the commit if it reports anything.
For CI, a GitHub Actions job can install ShellCheck from the runner's Ubuntu archive and check every script. Save it as .github/workflows/shellcheck.yml:
name: shellcheck
on: [push, pull_request]
jobs:
shellcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Install ShellCheck
run: sudo apt-get update && sudo apt-get install -y shellcheck
- name: Check shell scripts
run: |
shellcheck --version
shellcheck -f gcc scripts/*.shThe runner's ShellCheck may be older than 0.11, so the job prints its version first; if a finding appears in CI but not on your machine, compare the two versions. To fail the build only on warnings and errors while you clean up an old codebase, add -S warning, which hides info and style findings. Remove it once the backlog is gone.
Check it worked
shellcheck -f gcc scripts/*.sh prints nothing and then all scripts pass. In your own repository, a commit that adds an unquoted cd $dir is stopped by the hook with SC2086 and SC2164, and a push shows the same findings in the job log.
Next steps
To remove everything this tutorial created, delete the demo folder. Check the path first, as rm -rf does not ask:
rm -rf ~/shellcheck-demo ~/shellcheck-wiki- Write safer Bash scripts with strict mode and traps covers
set -euo pipefail, its gaps, and cleaning up withtrap. - Your first GitHub Actions workflow explains the workflow file the CI job above uses.
- Hello World in Bash is the place to start if any of the Bash here was new.
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Write least-privilege sudo rules with visudoYou will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials