HxHippy

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.

-c

Clear the history list by deleting all entries for the current session.

-d OFFSET

Delete 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).

!N

Re-execute the Nth command from the history list (history expansion).

!-N

Re-execute the Nth command from the end of the history list (history expansion).

!STRING

Re-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

$ history

Displays the entire command history with line numbers.

$ history 20

Displays the last 20 commands from the history.

$ history -c

Clears the command history for the current session.

$ history -d 105

Deletes the command at history entry 105.

$ history -a

Appends history from the current session to the default history file (e.g., ~/.bash_history).

$ history -w my_command_log.txt

Saves the current history to the file `my_command_log.txt`.

$ !!

Executes the last command again.

$ !15

Executes the 15th command from the history list.

$ !ls

Executes the most recent command that started with "ls".

$ Ctrl+R then type `git`

Interactively search backwards for commands containing "git".

$ history | grep sudo

Searches the command history for lines containing "sudo".

historycommandlogpreviousshellbashzshrecallbang