What does typing a single exclamation mark do in Bash?

The lone ! at the start of a command negates the exit status of the command or pipeline: if the command exits 0, it will flip into 1 (failure), and if it exits non-zero it will turn it into a 0 (successful) exit.

This use is documented in the Bash manual:

If the reserved word ‘!’ precedes the pipeline, the exit status is the logical negation of the exit status as described above.

A ! with no following command negates the empty command, which does nothing and returns true (equivalent to the : command). It thus inverts the true to a false and exits with status 1, but produces no error.


There are also other uses of ! within the test and [[ commands, where they negate a conditional test. These are unrelated to what you're seeing. In both your question and those cases it's not related to history expansion and the ! is separated from any other terms.


You can find the single exclamation point documented in the bash manual section 3.2.2 Pipelines

If the reserved word ‘!’ precedes the pipeline, the exit status is the logical negation of the exit status as described above.

$ ! true; echo $?
1
$ ! false; echo $?
0

Also section 3.2.4.2 Conditional Constructs

! expression

True if expression is false.


Also section 4.1 Bourne Shell Builtins

! expr

True if expr is false.


It also should be noted that ! will start history substitution unless followed by: a space, tab, the end of the line, ‘=’ or ‘(’ (when the extglob shell option is enabled using the shopt builtin).