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.

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

# 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:
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:
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:
[[ $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:

# 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:
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:
case $string in
    prefixA|prefixB)
        # Evaluates if $string matches either 'prefixA' or 'prefixB'
        ;;
    *suffixA|*suffixB)
        # Evaluates if $string ends with 'suffixA' or 'suffixB'
        ;;
esac
Master Bash with Deep Grasping Methodology!Learn More