tl;dr: ShellCheck is a bash/sh static analysis and linting tool. Paste a shell command or script on ShellCheck.net and get feedback about many common issues, both in scripts that currently fail and scripts that appear to work just fine.
There’s been a lot of progress since I first posted about it seven months ago. It has a new home on ShellCheck.net with a simplified and improved interface, and the parser has been significantly bugfixed so that parsing errors for correct code are now fairly rare.
However, the best thing is that it can detect a heaping bunch of new problems! This post mentions merely a subset of them.
Quiz: ShellCheck is aware of many common usage problems. Are you?
find . -name *.mp3
sudo echo 3 > /proc/sys/vm/drop_caches
PS1='\e[0;32m\$\e[0m '
find . | grep "*.mp3"
[ $n > 7 ]
[[ $n > 7 ]]
tr 'A-Z' 'a-z'
cmd 2>&1 > log
array=(1, 2, 3)
echo $10
[[ $a=$b ]]
[[ $a = $b ]]
progress=$((i/total*100))
trap "echo \"Time used: $SECONDS\"" EXIT
find dir -exec cp {} /backup && rm {} \;
[[ $keep = [yY] ]] && mv file /backup || rm file
ShellCheck gives more helpful messages for many Bash syntax errors
Bash says | ShellCheck points to the exact position and says |
---|---|
: command not found | Literal carriage return. Run script through tr -d ‘\r’ |
unexpected token: `fi’ | Can’t have empty then clauses (use ‘true’ as a no-op) |
unexpected token `(‘ | Shells are space sensitive. Use ‘< <(cmd)', not '<<(cmd)' |
unexpected token `(‘ | ‘(‘ is invalid here. Did you forget to escape it? |
echo foo: command not found | This is a . Delete it and retype as space. |
ShellCheck suggests style improvements
Code | ShellCheck suggestion |
---|---|
basename "$var" |
Use parameter expansion instead, such as ${var##*/} |
ls | grep 'mp3$' |
Don’t use ls | grep. Use a glob or a for loop with a condition. |
expr 3 + 2 |
Use $((..)), ${} or [[ ]] in place of antiquated expr. |
cat foo | grep bar |
Useless cat. Consider ‘cmd < file | ..' or 'cmd file | ..' instead. |
length=$(echo "$var" | wc -c") |
See if you can use ${#variable} instead |
ShellCheck recognizes common but wrong attempts at doing things
Code | ShellCheck tip |
---|---|
var$n=42 |
For indirection, use (associative) arrays or ‘read “var$n” <<< "value"'". (Bash says “var3=42: command not found”) |
${var$n}
|
To expand via indirection, use name=”foo$n”; echo “${!name}” (Bash says “bad substitution”. ) |
echo 'It\'s time' |
Are you trying to escape that single quote? echo ‘You’\”re doing it wrong’ (Bash says “unexpected end of file”) |
[ grep a b ] |
Use ‘if cmd; then ..’ to check exit code, or ‘if [[ $(cmd) == .. ]]’ to check output (Bash says “[: a: binary operator expected”) |
var=grep a b |
To assign the output of a command, use var=$(cmd) (Bash says “a: command not found”) |
ShellCheck can help with POSIX sh compliance and bashisms
When a script is declared with #!/bin/sh
, ShellCheck checks for POSIX sh compliance, much like checkbashisms
.
ShellCheck is free software, and can be used online and locally
ShellCheck is of course Free Software, and has a cute cli frontend in addition to the primary online version.
ShellCheck wants your feedback and suggestions!
Does ShellCheck give you incorrect suggestions? Does it fail to parse your working code? Is there something it could have warned about, but didn’t? After pasting a script on ShellCheck.net, a tiny “submit feedback” link appears in the top right of the annotated script area. Click it to submit the code plus your comments, and I can take a look!