Home > Software engineering >  bash case statement quoting behaves as documented, but please explain
bash case statement quoting behaves as documented, but please explain

Time:10-02

c.f. the case example here
Specifically:

$: g=a*[b]; case $g in $g) echo 'unquoted pattern' ;; "$g") echo 'quoted pattern' ;; esac
quoted pattern

A simple string behaves exactly as expected -

$: g=a; case $g in $g) echo 'unquoted pattern' ;; "$g") echo 'quoted pattern' ;; esac
unquoted pattern

I made sure there was no matching file to glob, no shopt options muddying the water, and tested the value explicitly:

$: echo @$g@ "@$g@" $g "$g"
@a*[b]@ @a*[b]@ a*[b] a*[b]
$: [[ $g == "$g" ]] && echo ok || echo no
ok

So why does it, in this case, choose the second option?
Shouldn't both evaluate to the same results?

CodePudding user response:

The case statement uses pattern matching, not string equality, to compare the word to each pattern.

The pattern a*[b] matches any string starting with an a, with zero or more characters followed by a single b. It does not match the string a*[b], because that string ends with a ], not a b.

  • Related