patternbashTip
ShellCheck: static analysis tool that catches common bash mistakes
Viewed 0 times
shellchecklintingstatic analysisbash mistakesCIcode qualitySC2086
Error Messages
Problem
Bash scripts have many subtle pitfalls (quoting, word splitting, portability). Manual review misses them. ShellCheck identifies hundreds of common mistakes with precise explanations.
Solution
Install ShellCheck and run it on every script. Integrate into CI.
# Install
brew install shellcheck # macOS
apt install shellcheck # Debian/Ubuntu
# Run
shellcheck myscript.sh
# In CI (GitHub Actions)
# Disable specific warning inline
# shellcheck disable=SC2086
echo $var # intentionally unquoted
# Specify shell explicitly when extension is missing
shellcheck -s bash myfile
# Install
brew install shellcheck # macOS
apt install shellcheck # Debian/Ubuntu
# Run
shellcheck myscript.sh
# In CI (GitHub Actions)
- uses: ludeeus/action-shellcheck@master
# Disable specific warning inline
# shellcheck disable=SC2086
echo $var # intentionally unquoted
# Specify shell explicitly when extension is missing
shellcheck -s bash myfile
Why
ShellCheck knows about hundreds of bash gotchas and maps each to a numbered SC code with a wiki explanation. It's free, fast, and understands the difference between bash, sh, dash, and ksh.
Gotchas
- ShellCheck reads the shebang to determine which shell to check for — always set it correctly
- SC2086 (unquoted variable) is the most common warning — understand it before disabling
- ShellCheck can be run in editor via plugins (VSCode, vim, emacs) for real-time feedback
- Some warnings are style-only (info level) — use --severity=warning to reduce noise
- ShellCheck does not catch runtime errors or logic bugs — only syntactic and semantic issues
Code Snippets
Common ShellCheck fixes
#!/usr/bin/env bash
# shellcheck disable=SC2034 # silence 'unused variable' for intentional cases
# Example of common ShellCheck fixes:
# SC2086: quote variables
file="my file.txt"
echo "$file" # not echo $file
# SC2006: use $() not backticks
date=$(date +%F) # not date=`date +%F`
# SC2164: use || exit with cd
cd /some/dir || exit 1
# SC2181: check exit code directly
if ! grep pattern file; then echo missing; fi
# not: grep ...; if [[ $? -ne 0 ]]; thenContext
Any bash scripting project, especially in CI/CD pipelines
Revisions (0)
No revisions yet.