Skip to content
Vidar Holen edited this page Sep 9, 2019 · 1 revision

Quote expansions in case patterns to match literally rather than as a glob.

Problematic code:

case $input in
  -       ) echo "Reading from stdin..." ;;
  $output ) echo "Input should be different from output" ;;
esac

Correct code:

case $input in
  -         ) echo "Reading from stdin..." ;;
  "$output" ) echo "Input should be different from output" ;;
esac

Rationale:

When unquoted variables and command expansions are used in case branch patterns, they will be interpreted as globs.

This can lead to some surprising behavior, such as case $x in $x) trigger;; esac not triggering in some cases, such as when x='Pride and Prejudice [1813].epub'.

To match the literal content of the variable or expansion, make sure to double quote the expansion.

Exceptions:

If you intended to match a dynamically generated pattern, you can ignore this suggestion with a directive.

Related resources:

  • SC2053, where the same effect can be seen with [[ $x = $x ]].
  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally