case
Execute code based on pattern matching
TLDR
Match a variable against string literals to decide which command to run
Combine patterns with |, use * as a fallback pattern
Allow matching multiple patterns
Continue to the next pattern's commands without checking the pattern
Display help
SYNOPSIS
case WORD in
PATTERN1 | PATTERN2)
COMMAND-LIST1
;;
PATTERN3)
COMMAND-LIST2
;;
*)
DEFAULT-COMMAND-LIST
;;
esac
PARAMETERS
N/A
The case construct is a shell keyword and does not accept traditional command-line parameters or options. Its behavior is defined by the WORD it evaluates and the PATTERNs it attempts to match against within the script.
DESCRIPTION
The case construct in shell scripting (like bash, sh, ksh) provides a multi-way branching mechanism, allowing a block of commands to be executed based on the pattern matching of a given WORD. It's often used as a cleaner alternative to a long chain of if/elif/else statements, especially when comparing a single variable against multiple possible values or patterns. The case statement evaluates an expression against a list of PATTERNs. When the first match is found, the corresponding COMMAND-LIST is executed. Unlike switch statements in some other languages, case statements in shells automatically 'break' after the first matching pattern's commands are executed, meaning there's no fall-through behavior by default. The PATTERNs can utilize shell globbing characters (*, ?, []).
CAVEATS
- Patterns are matched from top to bottom. The first matching pattern's COMMAND-LIST is executed, and then the entire case statement terminates.
- Patterns use shell globbing rules (filename expansion) rather than regular expressions. Be mindful of special characters like *, ?, [], and |.
- The ;; terminator is crucial. Omitting it would result in a syntax error for that specific pattern block. While some shells (e.g., Bash 4+) offer extensions like ;& (fall-through, execute next list) or ;;; (fall-through, test next list), the standard case uses ;; for termination.
- The *) pattern acts as a wildcard, matching anything if no other patterns have matched. It is good practice to include it as the last pattern for robust error handling or default behavior.
PATTERN MATCHING ORDER
Patterns are evaluated sequentially. The first pattern that matches the WORD triggers its associated COMMAND-LIST, and then the case statement completes. This means the order of patterns can be significant, especially if patterns overlap (e.g., a* and abc).
GLOBBING PATTERNS
Patterns inside case statements are typically standard shell globbing patterns, similar to those used for filename expansion. This includes:
* *: Matches zero or more characters.
* ?: Matches exactly one character.
* [chars]: Matches any one character from chars (e.g., [aeiou]).
* [!chars] or [^chars]: Matches any one character not from chars.
* |: Used to separate multiple patterns for a single command list (logical OR).
* Literal characters: Match themselves.
HISTORY
The case statement is a fundamental construct in Unix shell programming, tracing its origins back to the Bourne Shell (sh) in the late 1970s. It was designed to provide a structured way for multi-way branching, offering a cleaner and more readable alternative to nested if/elif statements for certain types of conditional logic. Its syntax and behavior have remained largely consistent across various POSIX-compliant shells, including ksh, bash, and zsh, cementing its place as a standard tool in shell scripting.