How to reuse command history

Overview

Bash remembers commands so you do not need to retype them. History is convenient, but a recalled command runs with its original power after you press Enter.

Recall commands

Press the Up and Down arrows to move through earlier commands. Edit the recalled line before running it.

history
history 20
!!
!42

Search and controls

  • Ctrl+R starts a reverse search; type part of a command
  • history 20 shows the latest twenty entries
  • !! expands to the previous command
  • !42 expands to history entry 42
  • history -c clears the current shell history list

Safety and privacy

Prefer arrow keys or reverse search because you can inspect and edit before executing. Be especially careful repeating commands containing sudo, redirection, or deletion.

Do not put passwords or access tokens directly on command lines; they may be stored in history and visible to other tools.