Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

History Command Guide for Bash, Sh & Zsh

Your terminal’s memory of every command.

What Is the History, and Why Use It?

The shell records every command you type into a history list, stored on disk (~/.bash_history for bash, ~/.zsh_history for zsh) and in memory. history lets you review it, re-run entries, search it, and rescue commands you already ran but can no longer remember typing.

Why Learn History?

  • Typing sudo systemctl restart nginx && systemctl status nginx twice is a waste of life; !42 runs it again.
  • Ctrl+R backward-search is the single most-used keyboard shortcut in this wiki.
  • History is a debugging log: when something broke yesterday, the command that broke it is in there.

History is the difference between a terminal you use and a terminal that remembers for you. Learn history, !!, and Ctrl+R, and your typing drops by half.

1. Listing and Running Commands

history              # numbered list of recent commands
history 20           # just the last 20
history | grep nginx # find when you touched nginx
!!                   # re-run the previous command
!42                  # re-run command number 42
!nginx               # re-run the most recent command starting with "nginx"
!$                   # the last argument of the previous command

2. Searching History

Ctrl+R               # start a reverse search; keep pressing to cycle
Ctrl+R, then type    # narrowing the query narrows the match
Ctrl+G               # abort the search

3. Controlling What Is Saved

export HISTSIZE=10000        # commands kept in memory
export HISTFILESIZE=20000    # lines saved to the file
history -d 42                 # delete one entry
history -c                    # clear the list entirely
history -w                    # write the list to the file now

4. Editing the Last Command

Alt+.                  # insert the last argument (bash)
^nginx^docker^         # re-run last command with text swapped

Common Pitfalls

MistakeFix
!! re-ran something dangerousThat is what !! does — review with history first
History missing after a rebootFlush it: history -a appends to the file before the session ends
Two terminals overwriting each other’s historyEnable histappend (zsh) / PROMPT_COMMAND history sharing (bash)
Can’t find an old commandhistory | grep pattern beats scrolling
Sensitive commands in the filehistory -d N removes single entries; consider HISTIGNORE
PageWhat It Covers
Environment VariablesHISTSIZE, HISTFILESIZE and friends
AliasesTurning frequent history entries into shortcuts
Keyboard ShortcutsCtrl+R, Ctrl+G, Ctrl+C in practice
Shell Scriptinghistory -c as a scripting hygiene habit
bash / sh / zshWhere each shell keeps its history file

History Cheatsheet

TaskCommand
List historyhistory
Last N entrieshistory N
Re-run previous command!!
Re-run command #N!N
Re-run last nginx command!nginx
Reverse searchCtrl+R
Delete one entryhistory -d N
Clear all entrieshistory -c
Save to file nowhistory -w
Last argument of previous command!$ or Alt+.