Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท System Information

Bash history command

Reference for the Bash history builtin: list, search, delete and save command history, expansion with ! and !!, HISTSIZE, HISTCONTROL and HISTFILE.

history is a Bash builtin that shows and manages the list of commands you have run. Bash keeps the list in memory while a session runs and writes it to a file (~/.bash_history by default) when the session ends, so the next session starts with your old commands available on the Up arrow and in Ctrl+R search.

History is a feature of interactive shells. A script has it switched off, so the examples below enable it explicitly with set -o history and use a temporary file instead of your real history.

Terminal
history [N]
history -c
history -d OFFSET
history -a | -n | -r | -w [FILE]

Try it

Bash Building a history and listing it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash History expansion and settings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
history [n]
history -c
history -d offset
history -d start-end
history -anrw [filename]
history -ps arg [arg ...]
Name Description
n Show only the last n entries.
-c Clear the in-memory list. The history file is untouched until the shell writes it.
-d offset Delete the entry with that number. Negative offsets count from the end (-1 is the last entry). start-end deletes a range (Bash 5.0 and later).
-a Append this session's new entries to the history file.
-n Read entries from the file that were added since the last read (other sessions).
-r Read the file and append its lines to the list.
-w Write the whole in-memory list to the file, overwriting it.
-p arg Perform history expansion on arg and print the result without storing it.
-s arg Store the arguments as one new history entry.

Returns: 0, or non-zero for an invalid option or an offset out of range.

Errors and edge cases: history -d with a number that does not exist prints "history position out of range". Entries with timestamps (see HISTTIMEFORMAT) are stored in the file as #epoch comment lines.

Usage notes

  • Variables that matter. HISTSIZE is the number of lines kept in memory, HISTFILESIZE the number kept in the file, HISTFILE the file name. HISTCONTROL=ignoreboth drops duplicates and commands that start with a space; HISTIGNORE='ls:cd:pwd' ignores listed commands; HISTTIMEFORMAT='%F %T ' shows timestamps.
  • Keep a secret out of history. With HISTCONTROL=ignorespace a command starting with a space is not saved. This is a convenience, not a security measure: other tools (the terminal scrollback, process lists, shell logs) may still see it.
  • Several terminals. By default each session writes its history when it exits and the last one to exit wins for ordering. shopt -s histappend makes sessions append instead of overwriting; adding history -a to PROMPT_COMMAND shares commands immediately (see shopt).
  • Event designators. !! repeats the last command, !$ is its last word, !n repeats entry n, !abc the latest command starting with abc, and ^old^new replaces text in the previous command. They are expanded before the shell parses the line, which makes them risky in pasted text: set histverify (shopt -s histverify) to review the line first.
  • Search. Ctrl+R searches backwards as you type; press it again for older matches. history | grep ssh finds a command whose exact form you forgot.
  • A subshell or script has no history. bash script.sh and $(history) in non-interactive shells show nothing unless you enable it as above.
  • Other shells keep history differently: zsh uses fc -l, and fc itself also exists in Bash for editing and re-running commands.