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

Run a command without adding it to history by adding a leading space
$ <Space>[command]
copy

SYNOPSIS

history [n]
history { -c | -d offset[..offset] | -a[n] | -n[filename] | -r[filename] | -w[filename] | -p | -s words... }

PARAMETERS

-a
    append new history lines to the history file

-c
    clear the history list by deleting all entries

-d offset[..offset]
    delete specified history entry(ies); positive from start, negative from end

-n [filename]
    read unread lines from history file (default HISTFILE) into memory

-p
    perform history expansion (!!, !n) on input lines, print without storing

-r [filename]
    read history lines from file (default HISTFILE), overwriting current list

-s words...
    append supplied words as a single new history entry

-w [filename]
    write current history to file (default HISTFILE), overwriting it

n
    print only last n entries if positive; combine with options

DESCRIPTION

The history command, a Bash shell builtin, views and manipulates the list of previously executed commands stored in memory and optionally in a file. It enhances productivity by allowing quick recall, editing, and reuse of past inputs, reducing typing errors and repetition.

Invoked without arguments, it prints the full history with line numbers. Specifying a positive integer n limits output to the last n entries; a negative n shows from the start. Options provide control: clear lists, delete entries, sync with files, perform expansions, or append new commands.

History behaves per-session in memory (sized by HISTSIZE) and persists via HISTFILE (default ~/.bash_history, sized by HISTFILESIZE). Lines append on logout or manually; duplicates may be ignored per HISTCONTROL. Timestamps appear if HISTTIMEFORMAT is set.

Common workflow: use history | grep keyword to search, or !! for quick reuse. Integrates with editing via fc. Secure shells may limit sharing to prevent info leaks.

CAVEATS

Bash builtin only (no /bin/history); per-shell memory differs from shared file; large histories slow shells; insecure sharing leaks commands; disabled via enable -n history.

KEY VARIABLES

HISTFILE: file path (~/.bash_history).
HISTSIZE: max in-memory lines.
HISTFILESIZE: max file lines.
HISTCONTROL: ignore dups/starts/spaces.

EXAMPLES

history 20: last 20 commands.
history -c && history -w: clear and save.
history -d 10: delete entry 10.
history | grep apt: search history.

HISTORY

Introduced in Bash 1.14 (1994) inspired by ksh/csh; enhanced in Bash 2.0+ with file sync (-a/-w), deletion (-d), expansion (-p/-s); Bash 3.0 added timestamps (HISTTIMEFORMAT), Bash 4.0 ignored patterns (HISTIGNORE). Widely used for interactive shells.

SEE ALSO

fc(1), bash(1)

Copied to clipboard