history expansion

Interactive shortcuts starting with an exclamation mark that reuse earlier commands, such as a double exclamation mark for the whole previous line.

In more detail

!$ is the last word of the previous line and !grep is the most recent line starting with grep. It happens before quoting is applied, even inside double quotes, which is why an exclamation mark in a double-quoted string can surprise you. It is switched off in scripts; add :p to print an expansion without running it.

Examples

sudo !!
!!:p

Related terms

Taught in

References