> ## 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 Indirect Expansion

The `!` (bang or exclamation) operator in Bash is a highly overloaded token whose behavior is strictly determined by its syntactic context. It functions primarily as a logical negator for exit statuses and expressions, a modifier for parameter expansion, a character class negator in pattern matching, and a trigger for interactive history expansion.

## 1. Pipeline Negation (Reserved Word)

When used as a reserved word at the beginning of a pipeline, `!` logically inverts the exit status (`$?`) of that pipeline. If the pipeline evaluates to `0` (success), the `!` forces the shell to return `1` (failure). If the pipeline evaluates to any non-zero value, `!` forces it to return `0`.

```bash theme={"dark"}
! command [arg ...] [ | command2 ... ]
```

## 2. Logical Negation (Test Constructs)

Within the `test` builtin (`[ ]`) or the extended test keyword (`[[ ]]`), `!` acts as a unary operator that inverts the boolean evaluation of the subsequent expression.

```bash theme={"dark"}
[ ! expression ]
[[ ! expression ]]
test ! expression
```

## 3. Arithmetic Logical Negation

Within arithmetic evaluation contexts, `!` functions as a logical NOT unary operator. It evaluates to `1` if the subsequent arithmetic expression evaluates to `0`, and evaluates to `0` if the expression evaluates to any non-zero value.

```bash theme={"dark"}
(( ! expression ))
$(( ! expression ))
```

## 4. Indirect Parameter Expansion

When used as a prefix inside parameter expansion braces, `!` introduces a level of indirection (dereferencing). The shell evaluates the value of the variable, treats that value as the name of a *second* variable, and expands to the value of the second variable.

```bash theme={"dark"}
${!parameter}
```

## 5. Array Index/Key Expansion

When applied to an array variable combined with the `@` or `*` subscript within parameter expansion, `!` modifies the expansion to yield the assigned indices (for indexed arrays) or keys (for associative arrays) rather than the array's values.

```bash theme={"dark"}
${!array_name[@]}
${!array_name[*]}
```

## 6. Variable Name Prefix Expansion

When combined with a string prefix and the `@` or `*` operators, `!` instructs the shell to expand to a list of all currently defined variable names that begin with the specified prefix.

```bash theme={"dark"}
${!prefix@}
${!prefix*}
```

## 7. Character Class Negation (Pathname Expansion)

In standard pathname expansion (globbing) and pattern matching, `!` placed immediately after an opening bracket `[` negates the character class. It matches any single character *not* enclosed in the brackets.

```bash theme={"dark"}
[!characters]*
[!a-z]*
```

## 8. Pattern Matching Negation (Extglob)

When the `extglob` shell option is enabled (`shopt -s extglob`), `!` is used in conjunction with parentheses to form a negated pattern matching operator. It matches anything that does *not* match the specified pattern(s).

```bash theme={"dark"}
!(pattern[|pattern...])
```

## 9. History Expansion (Interactive Shells)

In interactive shells (or when `set -H` is enabled), `!` is the history expansion character. It instructs the shell's history library to substitute text from previously executed commands into the current command line before lexical analysis occurs.

* **Event designators:** Refer to specific command lines.

```bash theme={"dark"}
!!          # The immediately preceding command
!n          # Command line number 'n'
!-n         # The current command line minus 'n'
!string     # Most recent command starting with 'string'
!?string[?] # Most recent command containing 'string'
```

* **Word designators:** Extract specific words from a designated history event. They are separated from the event designator by a colon `:` (which can be omitted if the word designator begins with `^`, `$`, `*`, `-`, or `%`).

```bash theme={"dark"}
!^          # The first argument (word 1) of the previous command
!$          # The last argument of the previous command
!*          # All arguments of the previous command (excluding the command word)
!:n         # The 'n'th word of the previous command
!event:n    # The 'n'th word of the specified 'event'
```

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