LinuxCommandLibrary

history

bash builtin command that displays or manipulates the command history list

TLDR

Display command history with line numbers

$ history
copy
Show the last N commands
$ history [20]
copy
Clear the entire history
$ history -c
copy
Delete a specific history entry by line number
$ history -d [42]
copy
Append current session history to the history file
$ history -a
copy
Read history file and append to current list
$ history -r
copy
Write current history to the history file
$ history -w
copy
Execute a command from history by number
$ ![42]
copy
Execute the previous command
$ !!
copy

SYNOPSIS

history [n]
history -c
history -d offset
history [-anrw] [filename]
history -ps arg

DESCRIPTION

history is a bash builtin command that displays or manipulates the command history list. The shell maintains a record of commands entered during a session, allowing quick access to previous commands.
Without arguments, history displays the numbered list of commands. With an integer argument n, it shows the last n entries. The history is loaded from HISTFILE at shell startup and can be written back with -w or automatically on exit.
History expansion allows re-executing previous commands: !! repeats the last command, !n executes command number n, !string executes the most recent command starting with string, and !?string matches any command containing string. Use Ctrl+r for interactive reverse search.

PARAMETERS

-c

Clear the history list
-d OFFSET
Delete the history entry at position offset
-d START-END
Delete the range of history entries between start and end
-a
Append new history lines to the history file
-n
Read history lines not already read from the file to current list
-r
Read the history file and append contents to the history list
-w
Write current history to the history file
-p
Perform history substitution on args and display without storing
-s ARG
Add args to the end of the history list as a single entry

ENVIRONMENT

HISTFILE

File where history is saved (default: ~/.bash_history)
HISTSIZE
Number of commands kept in memory for current session
HISTFILESIZE
Number of commands kept in the history file
HISTTIMEFORMAT
Format string for timestamps (strftime format)
HISTCONTROL
Controls what is saved (ignorespace, ignoredups, erasedups)
HISTIGNORE
Colon-separated patterns of commands to ignore

CAVEATS

Commands starting with a space are not saved if HISTCONTROL contains ignorespace. The history is per-shell and not shared between concurrent sessions unless explicitly synchronized with -a and -r. History files can contain sensitive information like passwords accidentally typed on the command line. Use HISTIGNORE to exclude sensitive commands.

HISTORY

Command history originated in the C shell (csh) developed by Bill Joy in the late 1970s at Berkeley. The bash implementation builds on the GNU Readline library, which provides history manipulation and editing capabilities. The history command and expansion syntax became a standard feature in Unix shells, allowing users to efficiently reuse and modify previous commands.

SEE ALSO

bash(1), fc(1), readline(3)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community