history
View command history
TLDR
Display the commands history list with line numbers
Display the last 20 commands (in Zsh it displays all commands starting from the 20th)
Display history with timestamps in different formats (only available in Zsh)
[c]lear the commands history list (only for current Bash shell)
Over[w]rite history file with history of current Bash shell (often combined with history -c to purge history)
[d]elete the history entry at the specified offset
SYNOPSIS
history [option]... [n] OR history -s arg [arg...] OR history -anrw [filename] OR history -p arg [arg...] OR history -c OR history -d offset OR history -d start-end
PARAMETERS
-c
Clear the history list by deleting all entries.
-d offset
Delete the history entry at position OFFSET.
-d start-end
Delete the history entries in range start-end
-a [filename]
Append new history lines to the history file. If FILENAME is given, append to that file, otherwise append to the history file specified by the HISTFILE variable.
-n [filename]
Read all history lines not already read from the history file. If FILENAME is given, read from that file, otherwise read from the history file named by the HISTFILE variable.
-r [filename]
Read the history file. If FILENAME is given, read from that file, otherwise read from the history file named by the HISTFILE variable.
-w [filename]
Write the current history to the history file. If FILENAME is given, write to that file, otherwise write to the history file named by the HISTFILE variable.
-p arg [arg...]
Perform history substitution on the ARGs and display the result without storing them in the history list.
-s arg [arg...]
Append the ARGs to the history list as a single entry.
n
Display the last N lines of history. If no options are given, the entire history is displayed.
--help
Display help message and exit.
--version
Display version information and exit.
DESCRIPTION
The `history` command in Linux is a powerful tool for recalling and manipulating previously executed commands. It provides a record of commands entered in the current shell session, allowing users to easily review, reuse, or modify past actions. It can display the history list with line numbers, allowing for easy reference. Beyond simple display, `history` also offers options to clear the history list, append to it, or write it to a file. This is very useful for scripting, documentation and auditing. The size of the history list is controlled by the `HISTSIZE` environment variable; the `HISTFILESIZE` enviornment variable configures how many lines are written to the .bash_history when the shell closes. Changes to HISTSIZE and HISTFILESIZE only affect future commands.
CAVEATS
The behavior of the `history` command and the storage of history are heavily influenced by shell-specific configurations (e.g., `.bashrc`, `.zshrc`). The exact options and their behavior might vary slightly between shells (bash, zsh, etc.). The `HISTCONTROL` environment variable can also influence what commands are saved in the history (e.g., ignoring duplicates or commands starting with a space).
HISTORY EXPANSION
The `history` command is also tightly linked to history expansion features within the shell. Characters like `!` and `$` allow users to refer to and reuse elements from previous commands. For example, `!!` re-executes the last command, while `!n` executes the nth command in the history list.
This is powerful for quickly repeating commands or modifying previously run commands without retyping them entirely. Note, for security reasons history expansion is not available when the shell is in restricted mode.
ENVIRONMENT VARIABLES
Several environment variables play a crucial role in controlling the `history` command's behavior. `HISTSIZE` determines the maximum number of commands stored in the in-memory history list. `HISTFILESIZE` dictates the maximum number of lines written to the history file (usually `.bash_history` in the user's home directory) when the shell exits. `HISTCONTROL` can be set to `ignoredups` (prevent saving consecutive duplicate commands), `ignorespace` (prevent saving commands starting with a space), or `ignoreboth` (a combination of both). The HISTFILE variable specifies where the history file is stored.
SECURITY CONSIDERATIONS
The history file contains a record of commands executed by a user. Sensitive information, such as passwords or API keys, might inadvertently be stored in the history. It's crucial to be mindful of what commands are being executed and to avoid storing sensitive data in plain text within commands. Regularly reviewing and cleaning the history file can help mitigate security risks.
HISTORY
The `history` command is a fundamental feature of interactive shells like bash and zsh. Its origins trace back to the early days of Unix, evolving alongside the development of interactive command-line interfaces.
Initially, history functionality was often simpler, providing basic recall of previous commands. Over time, it gained features like numbered entries, editing capabilities (through `fc`), and more sophisticated control over what commands are saved and how. The feature has been refined with versions improving performance and security over the years, and integrating with `readline` to enhance command line editing capabilities.
SEE ALSO
fc(1), readline(3)