Boost Your Shell Scripts: Master ShellCheck for Error‑Free Bash Coding
ShellCheck is a popular open‑source static analysis tool that scans Bash, sh, ksh, and dash scripts, highlighting syntax mistakes, unsafe commands, and logic flaws, while offering detailed explanations and fixes, and can be run via CLI, editors, CI pipelines, or Docker.
ShellCheck is a widely used open‑source static analysis tool that helps developers detect and correct errors in shell scripts written for Bash, sh, ksh, and dash. It identifies syntax problems, misuse of commands, common coding mistakes, permission issues, and logic errors that can cause scripts to behave unexpectedly.
Main Features
Broad inspection scope : Detects a wide range of common errors and traps.
Detailed feedback : Provides an explanation for each issue and how to fix it.
Easy to use : Can be run from the command line, integrated into editors/IDEs such as VS Code, Sublime Text, Vim, or used in CI/CD pipelines.
Cross‑platform : Works on Linux, macOS, and Windows.
Strong community support : Active open‑source community continuously adds features and improvements.
Usage
Run ShellCheck on a script file from the terminal: shellcheck yourscript.sh This prints all suggestions and warnings found in the script.
Installation
Linux : Install via package manager, e.g., sudo apt-get install shellcheck (Debian/Ubuntu).
macOS : Install with Homebrew: brew install shellcheck.
Windows : Install via Chocolatey: choco install shellcheck.
ShellCheck can also be run using Docker or directly online on its website.
Project Repository
The source code is hosted on GitHub:
https://github.com/koalaman/shellcheckTypical ShellCheck Fixes
Unquoted variable
# Bad example
echo $userinput
# ShellCheck suggestion
# SC2086: Double quote to prevent globbing and word splitting.
# Fixed version
echo "$userinput"Missing shebang
# Bad example
echo "Hello World"
# ShellCheck suggestion
# SC2148: Add a shebang.
# Fixed version
#!/bin/bash
echo "Hello World"Using undefined variable
# Bad example
if [ $name == "John" ]; then
echo "Hello, John!"
fi
# ShellCheck suggestion
# SC2154: name is referenced but not assigned.
# Fixed version
name="John"
if [ "$name" == "John" ]; then
echo "Hello, John!"
fiDeprecated back‑ticks
# Bad example
result=`ls`
# ShellCheck suggestion
# SC2006: Use $(...) notation instead of legacy backticked `...`.
# Fixed version
result=$(ls)Continue after command failure
# Bad example
cd some_directory
rm *
# ShellCheck suggestion
# SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
# Fixed version
cd some_directory || exit
rm *Incorrect if‑statement syntax
# Bad example
if [$var -eq 1]
then
echo "True"
fi
# ShellCheck suggestion
# SC1045: It's not 'if' condition, you need a space after '['.
# SC1073: Couldn't parse this test expression.
# Fixed version
if [ $var -eq 1 ]; then
echo "True"
fiProblematic brace expansion
# Bad example
echo {1..10}
# ShellCheck suggestion
# SC2035: Use ./<em>glob</em> or -- <em>glob</em> so names with dashes won't become options.
# Fixed version (unchanged but noted)
echo {1..10}Reading lines in a pipeline
# Bad example
cat file.txt | while read line; do
echo $line
done
# ShellCheck suggestion
# SC2094: Make sure not to read and write the same file in the same pipeline.
# Fixed version
while IFS= read -r line; do
echo "$line"
done < file.txtImproper echo of a variable
# Bad example
echo "Path: $PATH"
# ShellCheck suggestion
# SC2027: The surrounding quotes actually unquote this. Remove or escape them.
# Fixed version
echo "Path: " $PATH ""Checking command exit status
# Bad example
output=$(some_command)
if [ $? -ne 0 ]; then
echo "Command failed"
fi
# ShellCheck suggestion
# SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $.
# Fixed version
if ! output=$(some_command); then
echo "Command failed"
fiThese examples illustrate how ShellCheck helps identify and correct common shell‑script errors, improving script quality and maintainability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
