history(1)
Display or manipulate the command history list in the current bash session.
Synopsis
history [OPTION] [n]Description
The history command displays the list of commands previously entered in the current shell session, stored in the history buffer. Each command is numbered sequentially. You can re-execute previous commands, search through history, or clear it entirely.
History is typically saved to ~/.bash_history when the shell exits. The HISTSIZE variable controls how many commands are kept in memory, and HISTFILESIZE controls how many are saved to disk.
Common options
| Flag | What it does |
|---|---|
-c | Clear the history list (delete all entries) |
-d offset | Delete the history entry at the specified offset |
-a | Append new history lines to the history file |
-n | Read new history lines from the history file (since last read) |
-r | Read the entire history file and replace current history |
-w | Write current history to the history file |
-p | Perform history substitution and print; do not add to history |
-s | Add a command to the history without executing it |
Examples
Display all commands in the current session history with line numbers
historyShow the last 10 commands from history
history 10Search history for all commands containing 'ssh'
history | grep 'ssh'Re-execute the command at line 42 in history
!42Re-execute the most recent command that starts with 'grep'
!grepClear all history from the current session
history -cDelete the history entry at line 5
history -d 5Add 'echo test' to history without executing it
history -s 'echo test'