> ## 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 Extended Glob

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:

```bash theme={"dark"}
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.

```text theme={"dark"}
?(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.

```text theme={"dark"}

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

```bash theme={"dark"}

# 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)}"
}
```

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