From c87464817194945a539d35370935f463d9673f5c Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 8 Sep 2019 20:05:30 -0700 Subject: [PATCH] Created SC2254 (markdown) --- SC2254.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SC2254.md diff --git a/SC2254.md b/SC2254.md new file mode 100644 index 0000000..3cb7d3b --- /dev/null +++ b/SC2254.md @@ -0,0 +1,35 @@ +## Quote expansions in case patterns to match literally rather than as a glob. + +### Problematic code: + +```sh +case $input in + - ) echo "Reading from stdin..." ;; + $output ) echo "Input should be different from output" ;; +esac +``` + +### Correct code: + +```sh +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! \ No newline at end of file