LinuxCommandLibrary

!

Execute a previous command from history

TLDR

Substitute with the previous command and run it with sudo

$ sudo !!
copy

Substitute with a command based on its line number found with history
$ ![number]
copy

Substitute with a command that was used a specified number of lines back
$ !-[number]
copy

Substitute with the most recent command that starts with a string
$ ![string]
copy

Substitute with the arguments of the latest command
$ [command] !*
copy

Substitute with the last argument of the latest command
$ [command] !$
copy

Substitute with the last command but without the last argument
$ !:-
copy

Print last command that starts with a string without executing it
$ ![string]:p
copy

SYNOPSIS

!event[:modifier]
Where event can be:
!!
!n
!-n
!string
!?string?
And modifier can modify the selected command.

PARAMETERS

!!
    Refers to the last command entered.

!n
    Refers to command number n in the history list.

!-n
    Refers to the command n lines back from the current one.

!string
    Refers to the most recent command starting with string.

!?string?
    Refers to the most recent command containing string.

event:s/old/new/
    Refers to event and substitutes the first occurrence of old with new.

event:gs/old/new/
    Refers to event and substitutes all occurrences of old with new.

event:p
    Prints the command referred to by event but does not execute it.

event:h
    Removes the trailing path component, leaving the "head".

event:t
    Removes all leading path components, leaving the "tail".

DESCRIPTION

! is a powerful shell built-in feature used primarily for history expansion. It allows users to recall, reference, and execute previously entered commands from the shell's history list without retyping them entirely.

The basic syntax involves using the exclamation mark followed by a specifier. Common forms include:
!!: Executes the last command.
!string: Executes the most recent command that starts with string.
!n: Executes the command corresponding to history number n.
!-n: Executes the command n lines back from the current command.
!?string?: Executes the most recent command containing string.

History expansion greatly enhances productivity by simplifying the process of re-running or slightly modifying complex or frequently used commands. It requires the shell's history feature to be enabled and populated.

CAVEATS

Requires the shell history feature to be enabled (usually the default in interactive sessions).
Primarily works in interactive shells like Bash, Zsh, Ksh, etc.
The exact syntax and available modifiers can vary slightly between different shell implementations.
Using complex substitutions or event specifiers might require quoting or careful handling of special characters.

DISABLING HISTORY EXPANSION

History expansion can usually be disabled within a session using commands like set +H in Bash. To use a literal !, you can prefix it with a backslash (\!) or enclose it in single quotes ('!').

HISTORY

History expansion, including the use of !, originated in the C shell (csh) and was later incorporated into other popular shells like Bash, Zsh, and Ksh, becoming a standard feature for interactive shell use.

SEE ALSO

history(1), bash(1), zsh(1)

Copied to clipboard