case statement
A statement that compares one value against several glob patterns and runs the block for the first that matches.
In more detail
Each pattern ends with ) and each block with ;;, alternatives are separated by |, and * matches anything as a final default. esac, case backwards, closes it. It reads more clearly than a long chain of elif tests on the same value.
Example
case "$file" in *.txt | *.md) echo text ;; *) echo unknown ;; esacRelated terms
- if statement — A statement that runs a block of code only when a condition holds, optionally with an else block for when it does not.
- glob pattern — A pattern that matches names using * for any run of characters, a question mark for one character and [abc] for one of a set.
- getopts — The builtin that parses short command-line options such as -v, -vq and -f value, one option per call, in a while loop.
Taught in
- Conditionals: test, Brackets, Double Brackets, case — Bash Scripting & Command Line Automation
- Option Parsing With getopts — Bash Scripting & Command Line Automation