Bash history is the list of commands that the Bash shell records for an interactive session and saves to a per-user history file, usually ~/.bash_history, so that earlier commands can be recalled, searched, edited and executed again. On Linux it is one of the most used features of the shell and terminal environment, reached through the arrow keys, the reverse search shortcut Ctrl+R, the history builtin and history expansion with the exclamation mark.
The feature is provided by the GNU History library, which Bash shares with Readline. It is purely a convenience and audit aid for the user: it is not a security log, because the user can edit, disable or delete it at any time.
How Bash records and stores command history
Bash keeps history in two places at once: an in-memory list for the running shell and a file on disk. When an interactive shell starts, it reads the history file into memory. Each command line the user enters is appended to the in-memory list, and when the shell exits normally, the list is written back to the file.
This design explains a common surprise. Two terminal windows open at the same time each hold their own in-memory history, and by default the shell that exits last overwrites the file with its own list. Commands from the other window can therefore disappear unless the histappend option is enabled, which makes Bash append rather than overwrite.
The following table lists the shell variables that control this behavior and their typical defaults on common Linux distributions.
| Variable | Purpose | Typical default |
|---|---|---|
| HISTFILE | Path of the history file | ~/.bash_history |
| HISTSIZE | Entries kept in memory | 500 or 1000 |
| HISTFILESIZE | Lines kept in the file | 500 to 2000 |
| HISTCONTROL | Filters duplicates and space-prefixed lines | ignoreboth on many distributions |
| HISTIGNORE | Patterns that are never saved | Unset |
| HISTTIMEFORMAT | Adds timestamps to the history listing | Unset |
These variables are normally set in .bashrc, so they apply to every interactive shell. Setting HISTSIZE to a negative value in Bash 4.3 and later removes the limit entirely, which some administrators prefer for long-term recall.
Recalling and reusing commands
Bash offers two families of recall tools: interactive editing provided by Readline and history expansion performed by the shell before a command is executed. Readline tools show the command before it runs, while expansion tools substitute text immediately, which makes them faster but less forgiving.
The next table summarizes the techniques that experienced users rely on most often. In shell jargon the exclamation mark is called bang, and the expressions that start with it are event designators.
| Technique | What it does | Example |
|---|---|---|
| Up and Down arrows | Step through previous entries | Press Up twice |
| Ctrl+R | Incremental reverse search by substring | Ctrl+R then type ssh |
| history command | Prints numbered entries | history 20 |
| Bang plus a number | Runs the entry with that number | Bang followed by 512 |
| Double bang | Repeats the last command | sudo followed by double bang |
| Bang plus a prefix | Runs the latest command starting with that text | Bang followed by git |
| Bang dollar | Inserts the last argument of the previous command | cd followed by bang dollar |
| ^old^new | Repeats the last command with a substitution | ^stagng^staging |
| fc | Opens a past command in an editor before running it | fc 512 |
The histverify shell option makes expansion safer: when it is set, a line produced by history expansion is loaded into the editing buffer instead of running at once, so the user can review it. Appending the :p modifier to an event designator prints the expanded command without executing it.
Bash history compared with other shells
Every major Unix shell keeps command history, but the file format, sharing model and defaults differ. Users who move between Bash, Zsh and the Fish shell often notice these differences first when commands from parallel sessions appear or fail to appear.
| Shell | Default history file | Notable behavior |
|---|---|---|
| Bash | ~/.bash_history | Writes on exit, plain text lines |
| Zsh | Set by HISTFILE, often ~/.zsh_history | Optional shared history and extended format with timestamps |
| Fish shell | ~/.local/share/fish/fish_history | Saves immediately, YAML-like format, merges sessions |
| KornShell | ~/.sh_history | Shared file used by concurrent sessions |
Bash can approximate the immediate saving of Fish and Zsh. A common pattern places history -a in the PROMPT_COMMAND variable, which appends each command to the file as soon as the prompt returns. Adding history -n also reads lines written by other sessions, at the cost of a slightly mixed recall order.
Configuring history for daily work
A good configuration balances long, reliable recall against clutter and privacy. The defaults on many Linux distributions keep only a few hundred lines, which is often too little for administrators who rely on history to reconstruct earlier work.
The decision table below shows typical goals and the settings that achieve them.
| Goal | Recommended setting | Reason |
|---|---|---|
| Keep history from parallel terminals | shopt -s histappend | Prevents the last shell from overwriting the file |
| Save commands immediately | PROMPT_COMMAND with history -a | Survives crashes and closed windows |
| Know when a command ran | HISTTIMEFORMAT set to a date format | Timestamps are stored as comment lines |
| Reduce duplicate noise | HISTCONTROL=ignoreboth:erasedups | Drops repeats and space-prefixed lines |
| Skip trivial commands | HISTIGNORE with ls, pwd and exit | Keeps the list useful for search |
| Long-term recall | Large HISTSIZE and HISTFILESIZE | Older commands remain searchable |
With HISTCONTROL containing ignorespace, any command typed with a leading space is not recorded. This is the standard way to keep a single sensitive command out of history without disabling the feature for the whole session.
Privacy, security and common mistakes
The history file is a plain text file readable by its owner, and it frequently contains material that should not persist. Passwords passed as command-line arguments, API tokens in curl headers and database connection strings all end up in history unless the user prevents it.
- Secrets typed on the command line are stored in clear text; use prompts, environment files or a leading space instead.
- Wrong file permissions on
~/.bash_historycan expose the file to other users; mode 600 is the expected setting. - Treating history as an audit trail is a mistake, because users can run
history -c, unset HISTFILE or edit the file directly. - Careless use of history expansion with a short prefix such as rm can repeat a destructive command with unexpected arguments.
- Exclamation marks in double-quoted strings can trigger expansion unexpectedly in interactive shells; single quotes avoid this.
For genuine accountability, Linux systems use dedicated auditing tools such as the kernel audit subsystem, sudo logging or session recording. Bash history remains a personal productivity record, and deleting a single entry with history -d followed by history -w is the correct way to remove an accidentally recorded secret.
Explained without jargon
Bash history is like a notebook that the command line keeps for you. Every time you type a command and press Enter, the shell writes it down. Later, you can flip back through the notebook with the arrow keys or search it for a word, so you never have to retype a long command from memory.
The notebook is saved in a file in your home folder when you close the terminal. It belongs to you, which means you can tidy it, shorten it or erase pages. That is also why it should never hold passwords: anyone who can read the file can read what you typed.
A typical use case
A system administrator fixed a failing backup job on a Linux server three weeks ago with a long rsync command that included several exclusion patterns and a bandwidth limit. The same problem now appears on a second server. Instead of reconstructing the options, the administrator presses Ctrl+R, types rsync --bw, and the exact command appears within seconds.
Because the team had earlier set HISTTIMEFORMAT, histappend and a history size of 50,000 lines in a shared .bashrc, the entry was still present and showed the date it was run. The administrator copies the command, adjusts the destination host and documents it in the runbook. The outcome is a repair completed in minutes rather than an hour of reading manual pages, with no risk of a mistyped option.