Problem Statement
What is the case statement used for in bash?
Explanation
Case statement provides multi-way branching matching variable against patterns, cleaner than multiple if-elif for many conditions. Syntax:
```bash
case $VAR in
pattern1)
commands
;;
pattern2)
commands
;;
*)
default commands
;;
esac
```
Each pattern ends with ), commands end with ;;. Asterisk (*) is catch-all default.
Patterns support wildcards: abc matches exact string, a* matches strings starting with a, [abc] matches single character a, b, or c, {a,b,c} matches a, b, or c. Multiple patterns: pattern1|pattern2) matches either. Example:
```bash
case $OPTION in
start|begin)
echo "Starting"
;;
stop|end)
echo "Stopping"
;;
*)
echo "Unknown option"
;;
esac
```
Use ;& (fall-through) or ;;& (test next pattern) for advanced control in bash 4+. Case statements excellent for menu systems, command-line option parsing, or handling multiple conditions. More readable than long if-elif chains when checking single variable against many values.
