Linux Command Reference ยท Bash Syntax
Bash case statement
Reference for the Bash case statement: match a word against glob patterns, combine alternatives with |, use a default branch and the ;& and ;;& terminators.
case compares one word against a list of glob patterns and runs the commands of the first pattern that matches. It is the tidy replacement for a long if/elif chain that tests a single value, and it is the standard way to parse command-line options and to write init-style start|stop|restart scripts.
Patterns are shell globs, not regular expressions: * matches anything, ? one character, [a-z] a range, and | separates alternatives.
case WORD in pattern1) commands ;; pattern2|pattern3) commands ;; *) default commands ;; esac
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
case word in [(] pattern [| pattern]... ) list ;; ... esac
| Part | Description |
|---|---|
word |
Expanded (parameters, command substitution, arithmetic, quote removal), but not word-split or globbed. |
pattern |
A glob; quote parts of it to match them literally. Several patterns are joined by |. An optional opening ( is allowed. |
;; |
End of the branch; case finishes. |
;& |
Run the next branch's commands without testing its pattern (Bash 4.0+). |
;;& |
Go on testing the following patterns and run any further match (Bash 4.0+). |
Returns: the status of the last command executed in the matching branch, or 0 if no pattern matched.
Usage notes
- Patterns are tried in order; the first match wins (unless
;;&is used). Put the most specific patterns first and*)last. casedoes not need quotes around the word:case $var inis safe even if$varis empty or has spaces. Quote a pattern to match special characters literally, for example"*".- Matching is case-sensitive unless
shopt -s nocasematchis set. A bracket class such as[Yy]is the portable alternative. - Extended globs (
@(a|b),+(x),!(x),?(x),*(x)) needshopt -s extglobset before the case statement is parsed. Inside a function or script file, enable it on an earlier line. - A missing
;;before the next pattern is a syntax error. The very last branch may omit it, but writing it is a good habit. - The empty-string pattern
""matches an empty word, so a missing argument can be handled explicitly. casecannot test numeric ranges well; use(( ))or[[ ]]for those.casealso does not set variables for captured parts like a regular expression does.- The
)after a pattern can confuse editors inside$( case ... )in older Bash versions; writing the optional opening parenthesis(pattern)avoids this.