Lesson 6.6: Command history
How bash remembers commands, the ~/.bash_history file, reusing commands with !!, !N, !string and !$, searching with Ctrl+R, the HISTSIZE settings, and history as both a convenience and a security record.
13 min
You will learn to
- List and number your history
- Re-run commands with !!, !N and !string
- Reuse the last argument with !$
- Fix a forgotten sudo with sudo !!
- Explain where history is stored and what that means for secrets
Key terms
- Command history
- The list of commands bash keeps in memory during a session and saves to ~/.bash_history when the shell exits.
- History expansion
- Shortcuts that bash replaces with earlier commands before running: !! previous command, !N command number N, !-2 two commands ago, !text most recent starting with text, !$ last argument of the previous command.
- Ctrl+R
- Reverse incremental search: type part of an old command, press Ctrl+R again for older matches, Enter to run, Esc or Ctrl+G to cancel.
- HISTSIZE / HISTFILESIZE
- How many commands are kept in memory and in the history file.
← Pipes · All lessons · Command substitution →