> ## 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 Logical NOT

The `!` (bang) operator in Bash serves nine distinct syntactic functions depending on its execution context: logical negation in conditional expressions, arithmetic logical negation, pipeline exit status inversion, history expansion, indirect parameter expansion, variable name prefix matching, array key/index retrieval, character class negation in standard pathname expansion, and pattern negation in extended globbing.

## 1. Logical Negation (Test Constructs)

Within test commands (`[`, `[[`, and `test`), the `!` operator acts as a unary boolean NOT. It inverts the evaluation of the subsequent expression. If the expression yields a true result (exit status `0`), the `!` operator forces it to evaluate as false (exit status `1`), and vice versa.

**Syntax:**

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

## 2. Arithmetic Logical Negation

Within Bash arithmetic evaluation contexts (`$(( ))`, `(( ))`, and `let`), the `!` operator functions as a unary logical NOT for integers. It evaluates to `1` if the operand is `0`, and evaluates to `0` if the operand is any non-zero integer.

**Syntax:**

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

## 3. Pipeline Exit Status Inversion

When placed at the beginning of a pipeline (or a simple command), the `!` reserved word inverts the final exit status of that pipeline. If the pipeline completes with a success status (`0`), the `!` operator forces the pipeline's exit status to `1`. If the pipeline completes with any non-zero exit status, the `!` operator forces the pipeline's exit status to `0`. The shell subsequently reflects this inverted pipeline exit status in the `$?` special parameter. Because `!` is a shell reserved word in this context, it must be separated from the command by a valid token separator (such as a space, tab, or newline).

**Syntax:**

```bash theme={"dark"}
! command
! command1 | command2
```

## 4. History Expansion

In interactive shells (where `set -H` is enabled), the `!` character triggers the history expansion facility. It instructs the shell parser to substitute the text of a previously executed command into the current command line prior to tokenization and execution.

**Syntax:**

```bash theme={"dark"}
!!          # Expands to the immediately preceding command
!n          # Expands to command line number 'n' in the history list
!-n         # Expands to the command 'n' lines back in the history list
!string     # Expands to the most recent command starting with 'string'
!?string?   # Expands to the most recent command containing 'string'
```

*Note: History expansion can be escaped using a backslash (`\!`) or single quotes (`'!'`) to prevent premature evaluation.*

## 5. Indirect Parameter Expansion

When used as a prefix immediately inside a parameter expansion block (`${}`), the `!` operator triggers indirect expansion. The shell evaluates the value of the specified variable, treats that resulting string as a new variable name, and then expands to the value of that second variable.

**Syntax:**

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

## 6. Variable Name Prefix Matching

When combined with the `@` or `*` operators within a parameter expansion block, the `!` operator alters the behavior to expand to the names of variables rather than their values. It returns a list of all declared variable names that begin with the specified prefix.

**Syntax:**

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

## 7. Array Key/Index Expansion

When used as a prefix within a parameter expansion block alongside the `@` or `*` subscripts, the `!` operator retrieves the indices (for indexed arrays) or keys (for associative arrays) of the specified array instead of its values.

**Syntax:**

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

## 8. Pathname Expansion Character Class Negation

Within standard pathname expansion (globbing) and pattern matching, a `!` placed immediately after an opening bracket `[` negates the character class. It instructs the shell to match any single character that is not explicitly listed or contained within the specified range.

**Syntax:**

```bash theme={"dark"}
[!characters]
[!char1-char2]
```

## 9. Extended Globbing Pattern Negation

When the `extglob` shell option is enabled (`shopt -s extglob`), the `!` operator functions as a pattern negation prefix. It matches any string that does not match one or more of the patterns provided in the delimited list.

**Syntax:**

```bash theme={"dark"}
!(pattern)
!(pattern1|pattern2)
```

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