The case Statement & Pattern Matching
Branch cleanly on many values. Learn the case statement, glob patterns, multiple-pattern matches, and how it beats long if-elif chains.
Why case?
When a variable can take many values, a long if/elif/elif chain gets ugly. The case statement matches a value against patterns and is far more readable.
Basic Syntax
A case block tests a value against patterns, each ending in ) and a body terminated by ;;. The whole block ends with esac.
case "$fruit" in
apple) echo "red";;
banana) echo "yellow";;
esacAll lessons in this course
- Looping Constructs (for, while)
- Functions in Bash Scripts
- Script Arguments and Options ($@, $#, getopts)
- The case Statement & Pattern Matching