case Statements
Pattern matching
case compares a string against patterns. Each arm ends with ;;.
case "$1" in
start) echo "starting" ;;
stop) echo "stopping" ;;
*) echo "usage: $0 start|stop" >&2; exit 2 ;;
esac
Multiple patterns
Separate alternatives with |.
case "$ext" in
jpg|jpeg|png) echo "image" ;;
md|txt) echo "text" ;;
esac
Glob-style patterns
* and ? work in case patterns. Quote variables you expand as the word being matched.
Good for CLIs
Subcommands and option dispatch stay readable with case.
Comments
One comment per signed-in account. Comments are saved with this page’s URL.