18.4. Case statement

The case statement is a multiple-choice statement. You use case statements to match a value against a pattern. If the match is successful, you can execute commands based purely on this match. Here’s the format of the ‘ case ’ statement:

case value in pattern1)   commands1  ...  ;; pattern2)   commands2  ...  ;; esac 

Here’s how case works. The word after the value must be ‘in’, and each pattern must be terminated with a right bracket. The value can be a variable or a constant. When a match is found against a pattern all commands are then executed for that pattern up to the ;;.

The value will check each pattern for a match. Once a pattern is matched, it will not continue with the other patterns even after it has executed ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.