Bash history

Also known as: history command

On this page 7

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.

VariablePurposeTypical default
HISTFILEPath of the history file~/.bash_history
HISTSIZEEntries kept in memory500 or 1000
HISTFILESIZELines kept in the file500 to 2000
HISTCONTROLFilters duplicates and space-prefixed linesignoreboth on many distributions
HISTIGNOREPatterns that are never savedUnset
HISTTIMEFORMATAdds timestamps to the history listingUnset
Main variables that control Bash history

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.

TechniqueWhat it doesExample
Up and Down arrowsStep through previous entriesPress Up twice
Ctrl+RIncremental reverse search by substringCtrl+R then type ssh
history commandPrints numbered entrieshistory 20
Bang plus a numberRuns the entry with that numberBang followed by 512
Double bangRepeats the last commandsudo followed by double bang
Bang plus a prefixRuns the latest command starting with that textBang followed by git
Bang dollarInserts the last argument of the previous commandcd followed by bang dollar
^old^newRepeats the last command with a substitution^stagng^staging
fcOpens a past command in an editor before running itfc 512
Common ways to recall commands from Bash history

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.

ShellDefault history fileNotable behavior
Bash~/.bash_historyWrites on exit, plain text lines
ZshSet by HISTFILE, often ~/.zsh_historyOptional shared history and extended format with timestamps
Fish shell~/.local/share/fish/fish_historySaves immediately, YAML-like format, merges sessions
KornShell~/.sh_historyShared file used by concurrent sessions
History handling in common Unix shells

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.

GoalRecommended settingReason
Keep history from parallel terminalsshopt -s histappendPrevents the last shell from overwriting the file
Save commands immediatelyPROMPT_COMMAND with history -aSurvives crashes and closed windows
Know when a command ranHISTTIMEFORMAT set to a date formatTimestamps are stored as comment lines
Reduce duplicate noiseHISTCONTROL=ignoreboth:erasedupsDrops repeats and space-prefixed lines
Skip trivial commandsHISTIGNORE with ls, pwd and exitKeeps the list useful for search
Long-term recallLarge HISTSIZE and HISTFILESIZEOlder commands remain searchable
Bash history settings by goal

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_history can 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.