LinuxCommandLibrary

history

View command history

TLDR

Display the commands history list with line numbers

$ history
copy

Display the last 20 commands (in Zsh it displays all commands starting from the 20th)
$ history 20
copy

Display history with timestamps in different formats (only available in Zsh)
$ history -[d|f|i|E]
copy

[c]lear the commands history list
$ history -c
copy

Over[w]rite history file with history of current Bash shell (often combined with history -c to purge history)
$ history -w
copy

[d]elete the history entry at the specified offset
$ history -d [offset]
copy

Add a command to history without running it
$ history -s [command]
copy

SYNOPSIS

history [n]
history [-c | -d offset | -s entry | -r | -w | -a | -n | -p | -q]

PARAMETERS

n
    Displays the last n commands from the history list.

-c
    Clears the entire history list.

-d offset
    Deletes the history entry at the specified offset (position) in the list.

-s entry
    Adds entry to the end of the history list as a single entry.

-r
    Reads the history file and appends its contents to the current history list.

-w
    Writes the current history list to the history file, overwriting its contents.

-a
    Appends the 'new' history lines (those entered in the current session but not yet saved) to the history file.

-n
    Reads history lines not already read from the history file into the current history list.

-p
    Performs history substitution on the following arguments and displays the result without storing them in the history list.

-q
    Suppresses output when used with other options (e.g., `-a`, `-n`, `-r`, `-w`).

DESCRIPTION

The `history` command is a crucial shell built-in feature that allows users to view, manage, and re-execute previously entered commands. It significantly enhances productivity by reducing repetitive typing and providing an easy way to correct or re-run complex commands. Each command entered at the prompt is typically saved in a history list in memory and, upon shell exit, to a history file (commonly .bash_history for Bash). Users can navigate this history using arrow keys, search it incrementally (Ctrl+R), or manipulate it directly with the `history` command's various options. It's an indispensable tool for efficient command-line interaction, providing a persistent record of shell activities.

CAVEATS

The `history` command is a shell built-in (e.g., in Bash, Zsh, Ksh). Its behavior can vary slightly between shells, and it is not an external executable like most commands found in `/bin` or `/usr/bin`.
History size, file location, and saving behavior are largely controlled by shell environment variables (e.g., `HISTSIZE`, `HISTFILE`, `HISTFILESIZE`, `HISTCONTROL`).
Sensitive commands (e.g., containing passwords) can be stored in the history file, posing a security risk if the file is not properly secured. Consider using `HISTCONTROL=ignorespace` to prevent commands prefixed with a space from being saved, or `HISTCONTROL=ignoreboth` (ignorespace and duplicates).

ENVIRONMENT VARIABLES

Several environment variables control the behavior of the history list:
HISTSIZE: The maximum number of commands to store in the history list in memory (default 1000).
HISTFILE: The path to the history file where commands are saved (default ~/.bash_history).
HISTFILESIZE: The maximum number of lines contained in the history file (default 2000).
HISTCONTROL: Controls how commands are saved to the history list. Common values include `ignorespace` (don't save commands prefixed with a space), `erasedups` (remove duplicate entries), and `ignoreboth` (combines both).

HISTORY EXPANSION

Bash provides powerful history expansion capabilities using the `!` character, similar to csh. For example, `!!` re-executes the last command, `!ls` re-executes the last command starting with `ls`, and `!$` refers to the last argument of the previous command. This feature, enabled by default in interactive shells, can be controlled with `set -o histexpand` or `set -H`.

PERSISTENT HISTORY

For history to be saved across shell sessions, the `HISTFILE` variable must be set. Typically, the `shopt -s histappend` option is enabled to append new history entries to the existing file rather than overwriting it upon shell exit. Advanced configurations might use `PROMPT_COMMAND` to save history after each command, ensuring no loss of history in case of unexpected shell termination.

HISTORY

The concept of command history dates back to early interactive computing environments. In Unix, the C shell (csh) introduced a rudimentary `history` mechanism in the late 1970s, allowing users to recall and re-execute previous commands using event designators (like `!`). This feature proved immensely popular for improving command-line efficiency. Subsequent shells, notably the Korn shell (ksh) and the Bourne-Again shell (bash), built upon and significantly enhanced this capability. Bash's `history` built-in provides a robust set of options for managing the history list, reading from and writing to history files, and controlling history expansion, making it a cornerstone of modern shell interaction. Its design has evolved to handle persistent history across sessions and more sophisticated searching and manipulation.

SEE ALSO

fc(1), bind(1), shopt(1), set(1), bash(1)

Copied to clipboard