> ## 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 Nested Brace Expansion

Nested brace expansion is the recursive evaluation of Bash brace expressions (`{...}`) embedded within one another. The Bash parser evaluates these structures strictly **outside-in** using top-down recursive descent. It identifies the outermost brace expression, splits the contents by commas at the current nesting level (level 0), and recursively expands each resulting part left-to-right, preserving strict ordering without applying implicit sorting.

## Syntax and Lexical Parsing

During the parsing phase, Bash identifies unescaped braces and commas to determine the hierarchical depth of the expression. Brace expansion occurs strictly *before* variable, command, and arithmetic expansions.

```bash theme={"dark"}

# General syntax structure
{element1,prefix{inner1,inner2}suffix,element3}
```

When an outer brace expression is evaluated, Bash splits it into its constituent elements. If an element contains further nested braces, the parser recursively expands them, distributing any literal prefix or suffix characters across the generated inner strings.

## Expansion Mechanics

### 1. Hierarchical Comma Separation

Commas define the boundaries of elements at the current nesting level. An element can be a literal string, a sequence, or another brace expression.

```bash theme={"dark"}
$ echo {A,B{C,D}}
A BC BD
```

*Execution flow:*

1. The parser identifies the outermost brace and splits it by the level-0 comma into two elements: `A` and `B{C,D}`.
2. The first element `A` contains no further braces and remains `A`.
3. The second element `B{C,D}` is recursively evaluated. The parser splits `{C,D}` into `C` and `D`.
4. The prefix `B` is distributed to both, yielding `BC` and `BD`.

### 2. Deep Nesting

Nesting can occur at arbitrary depths. Because evaluation is strictly outside-in, the parser resolves the outermost structure first and recursively descends into deeper levels.

```bash theme={"dark"}
$ echo {1,2{3,4{5,6}}}
1 23 245 246
```

*Execution flow:*

1. The parser splits the outermost level into `1` and `2{3,4{5,6}}`.
2. The element `1` is fully resolved.
3. The element `2{3,4{5,6}}` is recursively expanded. The parser splits the next brace `{3,4{5,6}}` into `3` and `4{5,6}`, yielding `23` and `24{5,6}`.
4. The element `23` is fully resolved.
5. The element `24{5,6}` is recursively expanded. The parser splits `{5,6}` into `5` and `6`, yielding `245` and `246`.

### 3. Adjacent Nesting (Left-to-Right Expansion)

If multiple brace expressions are nested adjacently within a single outer element, Bash evaluates them left-to-right, outside-in.

```bash theme={"dark"}
$ echo {A,B{1,2}{X,Y}}
A B1X B1Y B2X B2Y
```

*Execution flow:*

1. The outermost brace is split into `A` and `B{1,2}{X,Y}`.
2. The element `A` is fully resolved.
3. In `B{1,2}{X,Y}`, the leftmost brace `{1,2}` is expanded first, yielding `B1{X,Y}` and `B2{X,Y}`.
4. The parser then recursively expands `{X,Y}` in each resulting string.
5. `B1{X,Y}` expands to `B1X` and `B1Y`.
6. `B2{X,Y}` expands to `B2X` and `B2Y`.

### 4. Nested Sequences

Sequence expressions (`{x..y}`) can be nested within comma-separated brace lists. The sequence is generated as a standard element during the recursive descent.

```bash theme={"dark"}
$ echo {X,Y{1..3},Z}
X Y1 Y2 Y3 Z
```

## Edge Cases and Limitations

* **Malformed Sequence Bounds:** Sequence expressions require hardcoded integers or single characters at the time of lexical analysis. Nesting a sequence inside a sequence bound (e.g., `{1..{2..5}}`) does not throw a syntax error or halt execution. Instead, the parser fails to identify the outer structure as a valid sequence. It falls back to standard expansion, evaluating the inner sequence and treating the outer characters as literals.

```bash theme={"dark"}
$ echo {1..{2..5}}
{1..2} {1..3} {1..4} {1..5}
```

* **Empty Elements:** An empty element within a nested brace is treated as a null string. It is a valid token and will be concatenated as an empty value during recursive expansion.

```bash theme={"dark"}
$ echo {A,B{C,}}
A BC B
```

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