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:
- Event designators, which identify the target line in the history list.
- Word designators, which identify the target word.
- 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.