> ## 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.

# Bash Case Statement

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

```bash theme={"dark"}
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.*

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
