Frequently Asked Question

How do I use shell history, Ctrl+R, !!, and !$?

Every interactive command you run is appended to a history file, by default ~/.bash_history. Press the up and down arrows to walk through previous commands. Press Ctrl+R to start a reverse incremental search: start typing and the shell jumps to the most recent matching command; press Ctrl+R again to step further back. Ctrl+G cancels the search.

Bash also supports history expansion: typing !! re-runs the last command (so sudo !! is the classic "I forgot sudo" fix), !1234 re-runs command number 1234 from the history list, !$ expands to the last argument of the previous command (so after mkdir /tmp/foo you can type cd !$), and !* expands to all arguments. Two variables tune the behaviour: HISTSIZE controls how many entries are kept in memory, HISTFILESIZE how many are saved to disk; setting HISTCONTROL=ignoredups skips duplicate consecutive entries.

Further reading and video