0

When we run !! in a shell session, it prints and run last executed command

I was wonder if it is an alias for another long-written bash built-in command and would like to know where it is defined

I also know we can interact with shell history with fc command.

So, what exactly is !!?

  • 3
    See the man page for bash, in the "HISTORY EXPANSION" section. – Gordon Davisson Mar 22 at 5:33
  • shopt -s histverify to make it copy to command line, but not yet run it. So you can read it and change it. – ctrl-alt-delor Mar 22 at 9:02
6

Bash natively provides a 'history' feature. Commands typed in on the shell are saved to a file, which allows each command to be recalled and executed at a later point.

Two built-in commands are used to work with this feature. The fc command is used to select one or more commands from history, modify them if required, and then execute them. The history itself is managed using the history command, which includes options to save or clear the command history.

Another part of this feature is history expansion, which is a way to re-use commands/arguments from history as part of input. To use history expansion, you would specify the history expansion character ('!' by default) followed by an identifier. This identifier can consist of three components, each separated by a colon (':'). The components are:

  1. Event designators, which identify the target line in the history list.
  2. Word designators, which identify the target word.
  3. Modifiers, which alter the expanded word.

Since event designators refer to specific lines, one of the forms in which they can be specified is !n - to refer to the nth line of history. Similarly, !-n refers to the nth line from the end of the file.

!! is defined as a synonym for !-1, which refers to the last line of history and therefore the last executed command.

For more details, see the Bash manual's section on Using History Interactively.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.