Skip to main content

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.

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.
! 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.
[ ! 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.
(( ! 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.
${!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.
${!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.
${!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.
[!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).
!(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.
!!          # 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 %).
!^          # 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'
Master Bash with Deep Grasping Methodology!Learn More