Linux Command Reference ยท Bash Syntax
Bash history expansion (!! and !$)
Reference for Bash history expansion: event designators like !! and !n, word designators like !$ and !*, modifiers such as :s, quick substitution ^a^b and how to turn it off.
History expansion lets you reuse words from earlier commands by typing short codes that start with an exclamation mark. Before Bash runs a line, it replaces these codes with text taken from the history list, shows you the expanded command, and then runs it. The best-known uses are sudo !! (repeat the last command with sudo) and !$ (the last word of the previous command).
It is a feature of interactive shells. It is off in scripts, because scripts have no history and a stray ! in a string would be a nasty surprise. Do not confuse it with $! (the PID of the last background job) or with the logical negation ! command.
!! # the previous command !$ # last word of the previous command !n # command number n ^old^new # repeat the last command with the first old replaced by new
Try it
Scripts do not expand ! on their own, but history -p runs the expansion and prints the result without executing it. That makes every designator easy to explore here. In your own terminal you simply type the codes at the prompt.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
event[:word][:modifier] # general form
| Designator | Meaning |
|---|---|
!! |
The previous command. |
!n, !-n |
Command number n, or n commands back. |
!string, !?string? |
Most recent command that starts with, or contains, string. |
^old^new^ |
Previous command with the first old replaced by new. |
:0, :1, :n |
The command word, the first argument, the n-th word. |
^, $, *, x-y |
First argument, last word, all arguments, a range of words. |
:h, :t, :r, :e |
Modifiers: directory part, file name, strip extension, keep only extension. |
:s/old/new/, :gs/old/new/ |
Substitute once or globally. |
:p |
Print the result and do not run it. |
:q |
Quote the words so they cannot be expanded again. |
Returns: the expanded line is executed in the normal way, so the status is that of the resulting command. An unknown event prints event not found and the line is not run.
Errors and edge cases: ! followed by a space, = or ( is not expanded, but inside double quotes an exclamation mark is, which is why echo "Hello!" can fail in an interactive shell. Single quotes protect it.
Usage notes
- Controlled by
set -H(on by default in interactive shells) orset +H/set +o histexpand. Many people putset +Hin~/.bashrcto avoid accidents with!in passwords and messages. - Use
:pwhen unsure.rm !$is dangerous if you forgot what the last word was;echo rm !$shows it first. sudo !!reruns the previous command as root. It is one of the safest and most useful shortcuts.- Related shortcuts that are not history expansion.
Alt+.in readline inserts the last argument of the previous line interactively, which is easier to check than!$.Ctrl+Rsearches history incrementally. See Readline shortcuts. - Escaping. A backslash (
\!) or single quotes keep the bang literal. A bang at the end of a double-quoted string, as in"Done!", is the most frequent surprise. - History size and file.
HISTSIZE,HISTFILESIZE,HISTCONTROL=ignoredupsandHISTTIMEFORMATcontrol what is recorded;historylists it andhistory -d Ndeletes an entry. - Not in scripts. Do not rely on
!!in scripts. Use variables or functions instead.