history Show the commands you ran before
Lists the commands you typed earlier in your shell, each with a number. Use it to find a long command you want to run again, or to search for something you ran last week. history is built into the shell, not a separate program.
Synopsis
history [N]
history -c
Common options
| Option or form | What it does |
|---|---|
N | Show only the last N commands, for example history 20 |
-c | Clear the history of the current shell session (bash) |
-d N | Delete entry number N from the history (bash) |
-w | Write the current session's history to the history file now (bash) |
-a | Append this session's new commands to the history file (bash) |
-r | Read the history file into the current session (bash) |
Examples
history 20
history | grep ssh
!482
!!
!scp
Notes
- history is a shell built-in, so which history finds nothing useful and man history may describe something else. Its behaviour and options depend on the shell. The flags above are for bash. In zsh, history is a shortcut for fc -l, and zsh does not support -c the same way.
- Press Ctrl+R in the shell to search the history as you type. It is often faster than history | grep.
- Anything you typed is in the history, including passwords or tokens written on the command line. Keep secrets out of commands, and delete a mistake with history -d N.
- Bash saves history to ~/.bash_history only when the session ends. Run history -a to save it sooner. Zsh uses ~/.zsh_history.
- !! and !N run a command straight away. Check what they will run first, for example by adding :p, as in !482:p, which prints without running.
See also
Last reviewed 2026-10-07.