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 (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.
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:- 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.
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:
- Requires
shopt -s extglobto 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.
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:
- 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 theBASH_REMATCHarray. - Quoting the right-hand side of the
=~operator forces literal string matching, which disables the|alternation operator.
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:
- The
|acts as a logical OR operator between standard glob patterns. - This alternation is native to the
casestatement’s parsing logic and does not requireextglobor regular expressions to be enabled. - The shell evaluates the alternations sequentially from left to right until a match is found.
Master Bash with Deep Grasping Methodology!Learn More





