What does typing a single exclamation mark do in Bash?

7,431

Solution 1

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.

Solution 2

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

Share:
7,431
ash
Author by

ash

computer science student. she/her. 🦉

Updated on September 18, 2022

Comments

  • ash
    ash almost 2 years

    Bash uses exclamation marks for history expansions, as explained in the answers to this question (e.g. sudo !! runs the previous command-line with sudo). However, I can't find anywhere that explains what running the following command (i.e. a single exclamation mark) does:

    !
    

    It appears to print nothing and exit with 1, but I'm not sure why it does that. I've looked online and in the Bash man page, but can't find anything, apart from the fact that it's a "reserved word" – but so is }, and running this:

    }
    

    prints an error:

    bash: syntax error near unexpected token `}'
    
    • jordanm
      jordanm about 6 years
      Nothing. ! indicates the "start" of a history expansion expression.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 6 years
    That's the meaning of ! as an argument to the test command (including bash's built-in version), not the meaning of ! when used in command position.
  • Michael Homer
    Michael Homer about 6 years
    It would need to be ! [...] to match the question in this case.
  • Yurij Goncharuk
    Yurij Goncharuk about 6 years
    Agreed with @Gilles and @Michael Homer. I knew that [] is test. I'm little hurried.
  • Jeff Schaller
    Jeff Schaller about 6 years
    +1 for the first answer to explain what is actually run and why that explains the return code.
  • Jeff Schaller
    Jeff Schaller about 6 years
    You noticed that there was no given string in the question...?
  • Hopping Bunny
    Hopping Bunny about 6 years
    Just to add, one can use !$ to use the last word of the previous command like so: ls -l /tmp cd !$ This will take you to the /tmp directory.