control-flow

case ... esac

Pattern-matches a word against glob-style patterns (not regex) and runs the first matching branch. Terminators control fall-through: ;; ends, ;& falls through, ;;& tries next pattern.

case WORD in PATTERN) ...;; PATTERN) ...;; *) ...;; esac

Common flags / operators

Flag / Operator Purpose
;; End the branch (no fall-through) — default
;& Fall through and execute the next branch's body
;;& Continue matching remaining patterns
*) Default catch-all pattern

Examples

case "$1" in start) do_start;; stop) do_stop;; *) echo usage;; esac

Classic init-style command dispatcher.

case "$file" in *.jpg|*.png) echo image;; *.txt) echo text;; esac

Multiple patterns per branch with |.

case "$var" in [0-9]*) echo starts_with_digit;; esac

Glob character classes work in patterns.

shopt -s nocasematch; case "$x" in yes) echo ok;; esac

Enable case-insensitive matching via nocasematch.

Gotcha

Patterns are shell globs, NOT regex — use [0-9] not \d, and * not .*. Always quote "$word" to avoid word splitting before matching.

Related

← All Bash builtins · Linux commands