history
Display command history
Overview
Displays or manipulates the command history list. It allows users to view previously executed commands, re-run them, or clear the history.
Syntax
history [n | -c | -d offset | -anrw [filename] | -ps arg ...] (Syntax and options can vary slightly between shells like bash and zsh)Common Options
[n]If n is a number, display only the last n history entries.
-cClear the history list by deleting all entries for the current session.
-d OFFSETDelete the history entry at the specified OFFSET (the number shown by `history`).
-a [FILENAME]Append new history lines (from this session) to the history file (default e.g., ~/.bash_history).
-n [FILENAME]Read history lines not yet read from the history file into the current list.
-r [FILENAME]Read the history file and append its contents to the current history list.
-w [FILENAME]Write the current history list to the history file, overwriting its contents.
-s ARG...Store the ARGs in the history list as a single entry, but do not execute them.
!!Re-execute the last command (history expansion).
!NRe-execute the Nth command from the history list (history expansion).
!-NRe-execute the Nth command from the end of the history list (history expansion).
!STRINGRe-execute the most recent command starting with STRING (history expansion).
!?STRING[?]Re-execute the most recent command containing STRING (history expansion).
Ctrl+R(Readline shortcut) Search backwards interactively through history (reverse-i-search).
Examples
Displays the entire command history with line numbers.
Displays the last 20 commands from the history.
Clears the command history for the current session.
Deletes the command at history entry 105.
Appends history from the current session to the default history file (e.g., ~/.bash_history).
Saves the current history to the file `my_command_log.txt`.
Executes the last command again.
Executes the 15th command from the history list.
Executes the most recent command that started with "ls".
Interactively search backwards for commands containing "git".
Searches the command history for lines containing "sudo".