$linuxjunkies
>

history(1)

Display or manipulate the command history list in the current bash session.

UbuntuDebianFedoraArch

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

FlagWhat it does
-cClear the history list (delete all entries)
-d offsetDelete the history entry at the specified offset
-aAppend new history lines to the history file
-nRead new history lines from the history file (since last read)
-rRead the entire history file and replace current history
-wWrite current history to the history file
-pPerform history substitution and print; do not add to history
-sAdd a command to the history without executing it

Examples

Display all commands in the current session history with line numbers

history

Show the last 10 commands from history

history 10

Search history for all commands containing 'ssh'

history | grep 'ssh'

Re-execute the command at line 42 in history

!42

Re-execute the most recent command that starts with 'grep'

!grep

Clear all history from the current session

history -c

Delete the history entry at line 5

history -d 5

Add 'echo test' to history without executing it

history -s 'echo test'