BASH Not In Case

This is contrary to the design of case, which executes only the first match. If you want to execute on multiple matches (and in your design, something which is 3 would want to execute on both 1 and 2), then case is the wrong construct. Use multiple if blocks.

[[ $i = 1 ]] || echo "1"
[[ $i = 2 ]] || echo "2"
[[ $i = 3 ]] || echo "3"
[[ $i = [1-3]* ]] && echo "! $i"

Because case only executes the first match, it only makes sense to have a single "did-not-match" handler; this is what the *) fallthrough is for.


You can do this with the extglob extension.

$ shopt -s extglob
$ case foo in !(bar)) echo hi;; esac
hi
$ case foo in !(foo)) echo hi;; esac
$

Tags:

Case

Bash