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

Alternation in Bash refers to the mechanism of specifying multiple possible string permutations or match patterns within a single expression. Depending on the parsing phase and the specific construct, alternation is evaluated either as a generative string expansion or as a logical OR operator for pattern matching.

Bash implements alternation across four distinct contexts: Brace Expansion, Extended Globbing (`extglob`), Regular Expressions, and `case` statements.

## 1. Brace Expansion Alternation

Brace expansion is a generative mechanism evaluated before variable expansion, command substitution, and filename generation (globbing). It uses comma-separated lists enclosed in curly braces to generate multiple discrete strings from a single token.

**Syntax:**

```bash theme={"dark"}
prefix{alt1,alt2,alt3}suffix
```

**Mechanics:**

* The shell computes the Cartesian product of the prefix, the alternations, and the suffix.
* Unquoted commas separate the alternations.
* An alternation element can be left empty (e.g., `{A,}`). This generates a null string for that specific permutation, effectively outputting the prefix and suffix without any intervening characters.
* Unquoted spaces act as token delimiters during the initial parsing phase. Including an unquoted space splits the construct into separate words before brace expansion can occur, completely preventing the expansion. To treat a space as a literal character within an alternation, it must be quoted or escaped.
* Nested alternation is supported and evaluated recursively.

**Syntax Visualization:**

```bash theme={"dark"}

# Single-level alternation
{A,B,C}        # Evaluates to three tokens: A, B, and C


# Alternation with prefix/suffix
pre{X,Y}post   # Evaluates to two tokens: preXpost and preYpost


# Escaped/Quoted spaces
{A,B\ C}       # Evaluates to two distinct tokens: A and B C


# Empty alternation
pre{A,}        # Evaluates to two distinct tokens: preA and pre


# Nested alternation
{A,B{1,2}}     # Evaluates to three tokens: A, B1, and B2
```

## 2. Extended Globbing (`extglob`) Alternation

When the `extglob` shell option is enabled, Bash extends standard pathname expansion (globbing) to support pattern alternation. This uses the pipe character (`|`) to separate alternate patterns within specific matching operators. Unlike brace expansion, this is a matching operation, not a generative one.

**Syntax:**

```bash theme={"dark"}
operator(pattern1|pattern2|pattern3)
```

**Mechanics:**

* Requires `shopt -s extglob` to be active.
* The `|` acts as a logical OR between the enclosed patterns.
* The behavior of the alternation depends on the preceding operator:
  * `@(...)`: Matches exactly one of the alternate patterns.
  * `?(...)`: Matches zero or one occurrence of the alternate patterns.
  * `*(...)`: Matches zero or more occurrences of the alternate patterns.
  * `+(...)`: Matches one or more occurrences of the alternate patterns.
  * `!(...)`: Matches anything *except* the alternate patterns.

**Syntax Visualization:**

```bash theme={"dark"}
shopt -s extglob


# Matches strings/files ending in exactly .extA or .extB

# Uses the standard '*' wildcard, a literal '.', and the '@(...)' alternation operator
*.@(extA|extB)
```

## 3. Regular Expression Alternation

Within the double-bracket conditional construct (`[[ ... ]]`), Bash supports Extended Regular Expressions (ERE) via the `=~` operator. Alternation here strictly follows POSIX ERE standards, using the pipe (`|`) as a logical OR operator to match alternate sub-expressions.

**Syntax:**

```bash theme={"dark"}
[[ $string =~ pattern1|pattern2 ]]
```

**Mechanics:**

* The alternation is evaluated by the system's regex engine, not the shell's globbing engine.
* Alternation can be scoped using parentheses `()`, which also creates capture groups accessible via the `BASH_REMATCH` array.
* Quoting the right-hand side of the `=~` operator forces literal string matching, which disables the `|` alternation operator.

**Syntax Visualization:**

```bash theme={"dark"}

# Unscoped alternation (matches 'prefixA' or 'B')
[[ $var =~ prefixA|B ]]


# Scoped alternation (matches 'prefixA' or 'prefixB')
[[ $var =~ prefix(A|B) ]]
```

## 4. `case` Statement Alternation

The `case` construct natively supports alternation for standard shell pattern matching. It uses the pipe character (`|`) to define multiple matching criteria for a single execution block.

**Syntax:**

```bash theme={"dark"}
case $variable in
    pattern1|pattern2)
        command_list
        ;;
esac
```

**Mechanics:**

* The `|` acts as a logical OR operator between standard glob patterns.
* This alternation is native to the `case` statement's parsing logic and does not require `extglob` or regular expressions to be enabled.
* The shell evaluates the alternations sequentially from left to right until a match is found.

**Syntax Visualization:**

```bash theme={"dark"}
case $string in
    prefixA|prefixB)
        # Evaluates if $string matches either 'prefixA' or 'prefixB'
        ;;
    *suffixA|*suffixB)
        # Evaluates if $string ends with 'suffixA' or 'suffixB'
        ;;
esac
```

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