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) 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:
Syntax and Operators
Extended globbing introduces five composite operators. Each operator wraps apattern-list, which consists of one or more standard shell patterns separated by a pipe character (|), functioning as a logical OR.
?(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
Thepattern-list evaluated inside the parentheses can contain literal strings, standard globbing characters (*, ?, [...]), or nested extended glob operators.
Evaluation Contexts
Once explicitly enabled, the Bash parser recognizes extended globbing operators during several distinct evaluation phases:- Pathname Expansion: Evaluated against the filesystem to generate lists of matching files or directories.
- Parameter Expansion: Evaluated during substring removal or replacement operations, such as
${variable#pattern},${variable%pattern}, or${variable//pattern/string}. - Conditional Constructs: Evaluated on the right-hand side of the
==or!=operators within[[ ]]test commands. - Case Statements: Evaluated as the matching criteria in
case ... inblocks.
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
Becauseextglob 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.
Master Bash with Deep Grasping Methodology!Learn More





