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.

Extended globbing (extglob) is a Bash shell option that augments standard pathname expansion and string matching with advanced, regular expression-like quantifiers and logical negation. It introduces composite operators into the shell’s native pattern matching engine, allowing for complex string evaluation. To use extended globbing, the feature must be explicitly enabled in the shell session or script using the shopt (shell options) builtin:
shopt -s extglob  # Enables extended globbing
shopt -u extglob  # Disables extended globbing

Syntax and Operators

Extended globbing introduces five composite operators. Each operator wraps a pattern-list, which consists of one or more standard shell patterns separated by a pipe character (|), functioning as a logical OR.
?(pattern-list)
*(pattern-list)
+(pattern-list)
@(pattern-list)
!(pattern-list)
Operator Definitions:
  • ?(pattern-list): Matches zero or one occurrence of the provided patterns. Functionally equivalent to the ? quantifier in standard regular expressions.
  • *(pattern-list): Matches zero or more occurrences of the provided patterns. Functionally equivalent to the * quantifier in standard regular expressions.
  • +(pattern-list): Matches one or more occurrences of the provided patterns. Functionally equivalent to the + quantifier in standard regular expressions.
  • @(pattern-list): Matches exactly one occurrence of the provided patterns. It enforces a strict match against one of the delimited options.
  • !(pattern-list): Matches anything except one of the given patterns. This provides logical negation for pattern matching.

Pattern List Mechanics

The pattern-list evaluated inside the parentheses can contain literal strings, standard globbing characters (*, ?, [...]), or nested extended glob operators.

# Syntax visualization of a pattern-list using the pipe delimiter
+(patternA|patternB|patternC)


# Syntax visualization of nested extended globs
!(patternA|*(patternB))

Evaluation Contexts

Once explicitly enabled, the Bash parser recognizes extended globbing operators during several distinct evaluation phases:
  1. Pathname Expansion: Evaluated against the filesystem to generate lists of matching files or directories.
  2. Parameter Expansion: Evaluated during substring removal or replacement operations, such as ${variable#pattern}, ${variable%pattern}, or ${variable//pattern/string}.
  3. Conditional Constructs: Evaluated on the right-hand side of the == or != operators within [[ ]] test commands.
  4. Case Statements: Evaluated as the matching criteria in case ... in blocks.
Note: In modern Bash environments, extglob must be explicitly enabled for all of the above contexts. (Bash 4.1 briefly enabled it by default for the == and != operators within [[ ]] constructs, but this behavior was reverted in Bash 4.3). If extglob is disabled, unquoted parentheses in pathname expansion, conditional constructs, and case statements will trigger a parse-time syntax error. However, in parameter expansion, a disabled extended glob does not cause a syntax error; the parser simply treats the characters (e.g., @(pattern)) as literal strings.

Parsing Caveat

Because extglob fundamentally alters how Bash tokenizes and parses code, the shopt -s extglob command must generally be executed before the parser reads the line or block containing the extended glob syntax. If the option is enabled on the same line or within the same compound command (such as a { ... } block or a function definition) where the extended glob is first used for pathname expansion, [[ ]] constructs, or case statements, the parser will throw a syntax error. This occurs because Bash parses the entire line or compound block before executing any of the commands within it, encountering unquoted parentheses before the shopt command is actually evaluated. This restriction does not apply to parameter expansion. Because parameter expansions are parsed as a single word and expanded at runtime, enabling extglob in the same compound command where a parameter expansion utilizes it is perfectly valid.

# INCORRECT: Parsed simultaneously; results in a syntax error for pathname expansion
shopt -s extglob; command @(patternA|patternB)


# INCORRECT: Parsed as a single compound block; results in a syntax error for pathname expansion
{
    shopt -s extglob
    command @(patternA|patternB)
}


# CORRECT: Parsed sequentially for pathname expansion
shopt -s extglob
command @(patternA|patternB)


# CORRECT: Parameter expansion is evaluated at runtime, making compound blocks valid
{
    shopt -s extglob
    echo "${variable#@(patternA|patternB)}"
}
Master Bash with Deep Grasping Methodology!Learn More