Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The Bash 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

case word in
    [ ( ] pattern1 )
        command_list_1
        ;;
    [ ( ] pattern2 | pattern3 )
        command_list_2
        ;;
    [ ( ] patternN )
        command_list_N
        ;;
    [ ( ] * )
        default_command_list
        ;;
esac

Mechanics and Evaluation

  1. 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.
  2. Pattern Expansion: Before matching is attempted against a specific clause, each pattern undergoes 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).
  3. Sequential Matching: Bash evaluates the expanded word against each expanded pattern sequentially from top to bottom.
  4. Pattern Matching Rules: Matching relies on Bash’s general Pattern Matching rules, not Regular Expressions and not Pathname Expansion. Unlike pathname expansion, wildcards (*, ?) in a case statement do match a slash (/) and a leading period (.).
  5. Case Sensitivity: By default, pattern matching is case-sensitive. Bash provides the nocasematch shell option to make pattern matching case-insensitive. This is enabled by executing shopt -s nocasematch prior to the case statement.
  6. Exit Status: The return code of a case statement is the exit status of the last command executed in the matched clause. If no patterns match the word, the exit status is 0.
  7. Termination: The statement concludes with the esac keyword (the reverse of case).

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 a case statement 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 either pattern1 or pattern2.
  • *: Matches any string of any length (including an empty string). Typically used at the end of the case statement 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 the case 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 entire case statement. 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 the word. This mimics the behavior of a C-style switch statement missing a break. 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 the word against the remaining patterns in the case statement. 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