Skip to content

Commit

Permalink
shellcheck: detect unbracketed if expressions in shell scripts
Browse files Browse the repository at this point in the history
Fail if `if $` or `if "$` appears in a shell script, since `if [ $` may
have been intended and the test will always return false if so.

This will generate false positives if a command is being run indirectly
via a shell variable.
  • Loading branch information
bgilbert committed Nov 1, 2022
1 parent 7967f62 commit 89f06f5
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/check_one.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ if [[ ${HASSHELLCHECK} == 1 ]]; then
if [[ $shebang =~ ^#!/.*/bash.* ]] || [[ $shebang =~ ^#!/.*/env\ bash ]]; then
shellcheck -x "$f"
bash -n "$f"
if grep -E '(^|[[:space:]])if[[:space:]]+"?\$' "$f"; then
echo "Possible unbracketed conditional in $f"
exit 1
fi
echo "OK ${f}"
fi
fi
Expand Down

0 comments on commit 89f06f5

Please sign in to comment.