Bash's extension to string equality tests for pattern matching is useful, but it can sometimes be more convenient to use the older POSIX-specified case construct, which is shell script's analogue of the C switch statement.
The case statement allows us to run commands based on the outcome of matching a string against glob patterns. For example, to check whether the command variable matches help, we might do this:
case $command in help) printf 'Command help:\n...' ;; esac
Note the following details of this syntax:
- You don't have to double-quote $command just after the case statement.
- The pattern to match has a closing ), but does not require an opening one.
- The closing keyword is esac, which is case spelled backwards, just as the closing keyword for if is fi.
- Each option is terminated with two semicolons.
We can add...