Skip to main content
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.

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.

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.

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.

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.

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.

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.

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

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.
  • 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 %).
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More