The BashDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
case statement is a multi-way branching control structure that evaluates a given string expression and executes a specific block of code based on pattern matching. It serves as a more efficient, readable alternative to deeply nested if-elif-else conditionals when comparing a single variable against multiple discrete values or wildcard patterns.
Syntax
Mechanics and Evaluation
- Word Expansion: The
word(expression) is expanded exactly once before any matching is attempted. This includes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal. - Pattern Expansion: Before matching is attempted against a specific clause, each
patternundergoes tilde expansion, parameter expansion, command substitution, and arithmetic expansion. Quote removal is not performed on the pattern. Instead, quotes are retained and interpreted by the pattern matcher to treat characters literally (e.g.,case $var in "*" )matches a literal asterisk because the quotes instruct the matcher to escape the*, not because the quotes were removed). - Sequential Matching: Bash evaluates the expanded
wordagainst each expandedpatternsequentially from top to bottom. - Pattern Matching Rules: Matching relies on Bash’s general Pattern Matching rules, not Regular Expressions and not Pathname Expansion. Unlike pathname expansion, wildcards (
*,?) in acasestatement do match a slash (/) and a leading period (.). - Case Sensitivity: By default, pattern matching is case-sensitive. Bash provides the
nocasematchshell option to make pattern matching case-insensitive. This is enabled by executingshopt -s nocasematchprior to thecasestatement. - Exit Status: The return code of a
casestatement is the exit status of the last command executed in the matched clause. If no patterns match theword, the exit status is0. - Termination: The statement concludes with the
esackeyword (the reverse ofcase).
Pattern Matching Operators
Patterns dictate the matching logic for each clause. Multiple patterns can be combined using the logical OR operator (|).
( pattern ): The opening parenthesis is optional but highly recommended when embedding acasestatement inside a command substitution (e.g.,$(case $x in (a) echo 1 ;; esac)). It ensures parentheses remain balanced, preventing syntax errors during parsing.text: Exact string match.pattern1 | pattern2: Matches eitherpattern1orpattern2.*: Matches any string of any length (including an empty string). Typically used at the end of thecasestatement as a default/catch-all clause.?: Matches any single character.[abc]: Matches any one character enclosed in the brackets.[a-z]: Matches any one character within the specified range.
Clause Terminators (Control Flow)
The behavior of thecase statement after a successful match is dictated by the terminator used at the end of the command list. Bash supports three distinct terminators:
-
;;(Standard Break): Terminates the command list and immediately exits the entirecasestatement. No further patterns are evaluated. This is the most common terminator. -
;&(Unconditional Fall-through — Requires Bash 4.0+): Executes the command list of the currently matched pattern, and then unconditionally executes the command list associated with the next pattern clause in the sequence, regardless of whether the next pattern actually matches theword. This mimics the behavior of a C-styleswitchstatement missing abreak. Warning: Using this operator on systems running older versions of Bash (such as macOS, which defaults to Bash 3.2) will result in a syntax error. -
;;&(Conditional Fall-through / Resume Matching — Requires Bash 4.0+): Executes the command list of the currently matched pattern, and then continues evaluating thewordagainst the remaining patterns in thecasestatement. If subsequent patterns match, their respective command lists are also executed. Warning: Using this operator on systems running older versions of Bash will result in a syntax error.
Master Bash with Deep Grasping Methodology!Learn More





