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.
history [N] history -c history -d OFFSET history -a | -n | -r | -w [FILE]
Try 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.
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
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.
HISTSIZEis the number of lines kept in memory,HISTFILESIZEthe number kept in the file,HISTFILEthe file name.HISTCONTROL=ignorebothdrops 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=ignorespacea 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 histappendmakes sessions append instead of overwriting; addinghistory -atoPROMPT_COMMANDshares commands immediately (seeshopt). - Event designators.
!!repeats the last command,!$is its last word,!nrepeats entryn,!abcthe latest command starting withabc, and^old^newreplaces text in the previous command. They are expanded before the shell parses the line, which makes them risky in pasted text: sethistverify(shopt -s histverify) to review the line first. - Search.
Ctrl+Rsearches backwards as you type; press it again for older matches.history | grep sshfinds a command whose exact form you forgot. - A subshell or script has no history.
bash script.shand$(history)in non-interactive shells show nothing unless you enable it as above. - Other shells keep history differently:
zshusesfc -l, andfcitself also exists in Bash for editing and re-running commands.
Related
alias,shopt,set,clear- Lessons: Shell Basics, Environment, Bash Completion and Prompts