history
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
The history command manages the history of commands executed in the shell. You can reuse, search, edit, and delete past commands. History is saved to ~/.bash_history and written to disk when the shell exits.
Syntax
Display the full history list with line numbers.
history
Show only the most recent N entries.
history N
Re-run the command with the given history number.
!number
Re-run the previous command.
!!
Re-run the most recent command that starts with the given string.
!string
Press Ctrl+R to start a reverse incremental search (reverse-i-search). As you type, the most recent matching command appears. Press Ctrl+R again to step back through older matches. Press Enter to execute or Ctrl+G to cancel.
Ctrl+R
Option List
| Option | Description |
|---|---|
| history | Display the full numbered history list. |
| history N | Display the most recent N history entries. |
| history -c | Clear all entries from the in-memory history. The file is not affected. |
| history -d N | Delete the history entry at position N. |
| history -w | Write the current in-memory history to ~/.bash_history immediately. |
| history -r | Read ~/.bash_history and append its contents to the in-memory history. |
| !number | Execute the history entry at the given number. |
| !! | Re-execute the most recent command. |
| !string | Execute the most recent command that starts with the given string. |
| !?string | Execute the most recent command that contains the given string. |
Environment Variable List
| Variable | Description |
|---|---|
| HISTSIZE | Maximum number of entries to keep in memory. Default is 500 or 1000 (varies by distribution). |
| HISTFILESIZE | Maximum number of lines to write to ~/.bash_history. Default is 500 or 2000 (varies by distribution). |
| HISTCONTROL | Controls what gets recorded. Options: ignoredups (skip duplicates), ignorespace (skip commands starting with a space), erasedups (remove all previous duplicates), ignoreboth (both ignoredups and ignorespace). |
| HISTIGNORE | Colon-separated list of patterns for commands to exclude from history. Wildcards (*) are supported. Example: HISTIGNORE="ls:pwd:exit" |
| HISTTIMEFORMAT | When set, timestamps are recorded with each history entry. Uses strftime format. Example: HISTTIMEFORMAT="%Y-%m-%d %T " |
| HISTFILE | Path to the file where history is saved. Default is ~/.bash_history. |
Sample Code
Display the most recent 10 history entries.
history 10 491 grep "Kogami Shinya" crime_log.txt 492 cat dominator_access.log 493 wc -l case_archive.log 494 ssh kogami@10.0.0.1 495 cat sibyl_query.sh 496 chmod +x sibyl_query.sh 497 ./sibyl_query.sh 498 ls -la /var/log/psycho_pass/ 499 diff crime_log.txt case_archive.log 500 history 10
Re-run the command at history number 492.
!492 cat dominator_access.log 2040-04-01 09:12:33 Tsunemori Akane LETHAL_ELIMINATOR granted 2040-04-01 09:18:47 Kogami Shinya PARALYZER granted
Re-run the previous command with !!.
!! cat dominator_access.log 2040-04-01 09:12:33 Tsunemori Akane LETHAL_ELIMINATOR granted 2040-04-01 09:18:47 Kogami Shinya PARALYZER granted
Re-run the most recent command starting with diff.
!diff diff crime_log.txt case_archive.log
Delete history entry number 493.
history -d 493
Clear all in-memory history. Since it has not been written to file yet, the file is unaffected.
history -c history
Write the current in-memory history to the file immediately (normally this happens only when the shell exits).
history -w
Add the following settings to ~/.bashrc to enable timestamped history.
~/.bashrc
export HISTTIMEFORMAT="%Y-%m-%d %T " export HISTSIZE=10000 export HISTFILESIZE=20000 export HISTCONTROL=ignoreboth
The following example demonstrates this:
source ~/.bashrc history 5 496 2040-04-01 09:05:11 chmod +x sibyl_query.sh 497 2040-04-01 09:06:03 ./sibyl_query.sh 498 2040-04-01 09:10:44 ls -la /var/log/psycho_pass/ 499 2040-04-01 09:14:55 diff crime_log.txt case_archive.log 500 2040-04-01 09:20:00 source ~/.bashrc
Configuration to share history across multiple terminals and append to the file on each exit.
~/.bashrc
# Append to history file instead of overwriting shopt -s histappend # After each command: write to file, clear memory, reload from file PROMPT_COMMAND="history -a; history -c; history -r"
Overview
Bash history has a two-tier structure: in-memory history (the current session) and file-based history (~/.bash_history). By default, the file is written when the shell exits. Use history -w to write immediately and history -r to reload from the file.
Bang expansion (!) executes commands without confirmation. Before running !! or !string, you can preview the command using !!:p (print the previous command without executing) or !string:p to avoid surprises.
Setting HISTCONTROL=ignorespace prevents a command from being recorded by prefixing it with a space. This can be useful when handling sensitive information temporarily. However, see the common mistake below for an important caveat.
Common Mistakes
Common Mistake 1: History gets overwritten across multiple terminals
With Bash's default settings, when a shell exits, it overwrites ~/.bash_history with the current session's history. When multiple terminals are open simultaneously, only the history of the last terminal to close is preserved — commands run in other terminals are lost.
echo $HISTFILE /home/kogami/.bash_history
Add the following to ~/.bashrc to make all terminals append to the shared history file instead of overwriting it.
~/.bashrc
# Append mode (default is overwrite) shopt -s histappend # After each command: write to file, clear memory, reload from all terminals PROMPT_COMMAND="history -a; history -c; history -r"
history -a appends new entries from the current session to the file. history -c clears in-memory history. history -r reloads the file back into memory. Together, these three keep all open terminals in sync with the same up-to-date history.
Common Mistake 2: Passwords typed on the command line end up in history
Passing a password directly as a command argument stores it in ~/.bash_history in plain text. This creates a leak risk if another user shares the same machine or if the home directory is accidentally included in a backup.
mysql -u ginoza -pS1byl_P4ss database_sibyl
The command above is recorded in history exactly as typed. Several countermeasures are available.
Fix 1: Prefix the command with a space and enable HISTCONTROL=ignorespace.
~/.bashrc
export HISTCONTROL=ignoreboth
The following example demonstrates this:
mysql -u ginoza -pS1byl_P4ss database_sibyl
The leading space (the very first character on the line) prevents this command from being recorded. However, this only works when HISTCONTROL is configured correctly — if the setting is missing, the command is still saved.
Fix 2: Do not put the password in the command arguments. Use a config file, environment variable, or interactive prompt instead.
mysql -u masaoka -p database_sibyl Enter password:
Specifying -p without a value causes an interactive password prompt. The input is not recorded in history. This approach keeps the password out of history entirely.
Fix 3: If the password has already been recorded, delete it with history -d and flush the change to disk with history -w.
history | grep mysql 342 mysql -u ginoza -pS1byl_P4ss database_sibyl history -d 342 history -w
If you find any errors or copyright issues, please contact us.