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) 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:
[ ! 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:
$(( ! 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:
! 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:
!!          # 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:
${!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:
${!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:
${!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:
[!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:
!(pattern)
!(pattern1|pattern2)
Master Bash with Deep Grasping Methodology!Learn More