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 all of 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-designator[:word-modifier][:history-modifier]

PARAMETERS

!!
    Expand to the last executed command.

!n
    Expand to command line n from history.

!-n
    Expand to nth command backward from current.

!string
    Most recent command starting with string.

!?string?
    Most recent command containing string.

!$
    Last argument of previous command.

!^
    First argument of previous command.

!*
    All arguments of previous command (except 0th).

!:N
    Nth argument of previous command.

!:N-M
    Arguments N through M of previous command.

DESCRIPTION

The ! character in Linux shells like Bash enables history expansion, allowing quick reuse of previous commands or arguments without retyping. When the histexpand shell option is set (default for interactive non-login shells), the shell scans input lines for ! and expands them to historical events before execution or placement in the command history.

History expansion recognizes event designators to select commands (e.g., !! for the last command, !n for line n) and modifiers to alter the selected text (e.g., :p to print without executing, :h for head directory). This feature boosts efficiency for repetitive tasks but requires caution to avoid unintended expansions.

For example, typing ls !* expands !* to all arguments of the previous command. Expansion occurs after parsing but before execution, and quick substitution (^old^new^) provides a shorthand alternative. Disable with set +H if needed.

CAVEATS

Expansions occur on unquoted !; use single quotes to prevent. Can cause syntax errors or security risks (e.g., command injection). Disabled in scripts by default. Use history -c to clear history.

COMMON MODIFIERS

:p print line without executing
:h remove trailing filename (/)
:t remove leading path
:r remove trailing suffix (.ext)
:e remove trailing suffix only
:x quote each word
:* all args from 1 to end
:% tail from unique arg.

VIEW HISTORY

Use history to list events. Expand with !# for current line, !?# for word at cursor.

HISTORY

Originated in Bill Joy's csh (1978) for C shell. Adopted by Bash (1989) with extensions; POSIX sh lacks it natively but supports via fc. Evolved with readline library for better completion.

SEE ALSO

bash(1), history(1), fc(1), csh(1)

Copied to clipboard