When working with Linux, particularly in environments where multiple terminal windows are common, such as in programming or system administration, it’s crucial to have access to your command history across all sessions. By default, the Bash shell does not immediately share history between its instances. This can be inconvenient when you need to recall commands executed in another terminal window. However, there are ways to configure Bash to share its history more effectively.
How to Configure Shared Bash History
To make your Bash history immediately accessible across multiple terminal windows, you need to modify how Bash handles its history file (~/.bash_history). Here’s a step-by-step guide to achieve this:
- Edit the Bash Configuration File:
Open your
.bashrcfile located in your home directory. You can do this with any text editor; here, we’ll usenano:
nano ~/.bashrc- Add or Modify the Following Settings:
Insert these lines at the end of the
.bashrcfile:
# Avoid duplicates HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it shopt -s histappend
# After each command, append to the history file and reread it export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"Here’s what each line does:
-
HISTCONTROL=ignoredups:erasedups: Prevents duplicate entries. -
shopt -s histappend: Makes Bash append the history instead of overwriting it. -
PROMPT_COMMAND: This is executed just before the Bash prompt is displayed. The commandshistory -aappends the session’s history to the file,history -cclears the current session’s history, andhistory -rreads the history file. -
Save and Exit: After adding the above lines, save the changes and exit the editor. For
nano, you can do this by pressingCTRL+X, thenYto confirm, andEnterto exit. -
Apply the Changes: To apply the changes, either close and reopen your terminal windows or source the
.bashrcfile in all open terminals:
source ~/.bashrcPros and Cons of Sharing Bash History
Pros:
-
Command Recall: Easily recall commands executed in any terminal window.
-
Efficiency: Improves workflow by not having to retype or remember commands executed in different sessions.
-
Troubleshooting: Helps in troubleshooting issues by providing a complete history across sessions.
Cons:
-
Conflicts: Rapid commands in different terminals might lead to out-of-order history entries.
-
Performance: Constantly writing to the history file can potentially slow down the terminal if dealing with very large history files or very frequent commands.
Tips for Managing Bash History
- Manage History Size: You can control the size of the history file by setting
HISTSIZEandHISTFILESIZEin your.bashrcfile. For example:
export HISTSIZE=10000 export HISTFILESIZE=20000-
Ignore Specific Commands: To prevent certain commands from being saved in history (like passwords), prefix them with a space if
HISTCONTROLis set to ignorespace or ignoredups. -
Secure Your History: Be cautious about storing sensitive commands in history. Commands containing passwords or other sensitive data should ideally not be recorded.
Related Reading
- Linux Bash Tips and Tricks pt1
- Understanding printf vs echo in Bash
- Here Documents vs Here Strings in Bash
- Bash for loops sequential counting
- Bulk File Renaming on Linux: rename, vidir, fd
What Breaks This (and Why It’ll Happen to You)
The setup above works great — until it doesn’t. A few things will quietly murder your shared history and leave you wondering why Ctrl+R is coming up empty.
PROMPT_COMMAND collisions
If something else in your .bashrc also sets PROMPT_COMMAND — a conda init block, a virtualenv hook, starship, oh-my-bash, whatever — it’ll clobber yours. The last assignment wins, and your history commands silently disappear. The fix is to append to it, not assign:
# WRONG — blows away anything else that set PROMPT_COMMANDexport PROMPT_COMMAND="history -a; history -c; history -r"
# RIGHT — safe to chain with other hooksexport PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND:-}"The ${PROMPT_COMMAND:-} expands to an empty string if it wasn’t set, so you don’t get a dangling semicolon running nothing.
tmux and screen sessions
Inside a tmux pane, source ~/.bashrc reloads the file but your existing panes inherited the old environment at attach time. History sharing works going forward but older panes may not have picked up the PROMPT_COMMAND change. The easy fix: just open a new window in your tmux session. Or, if you’re deep in a session and need it now:
# Reload history config in the current pane onlyexport PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND:-}"shopt -s histappendAlias that if you’re in tmux all day.
The sudo su trap
Dropping into a root shell with sudo su or sudo -i loads root’s .bashrc, not yours. Your carefully tuned HISTAPPEND setup is gone and root’s history goes to /root/.bash_history. If you need this to work under root as well, you need the same config in /root/.bashrc — or switch your habit to sudo -E bash when you need environment inheritance (though that’s its own can of worms).
Remote SSH sessions
ssh user@server pulls the remote machine’s .bashrc. If your server doesn’t have the same config, your history lives in the remote’s ~/.bash_history and vanishes when the session drops. This is expected behavior, not a bug — but it trips people up when they’re hopping between boxes all day. The practical solution is keeping a minimal version of the history config in your dotfiles repo and bootstrapping it on new servers.
The history file gets corrupted
Rare, but if two shells write simultaneously at exactly the wrong moment, ~/.bash_history can end up with a partial write. You’ll notice history starts returning garbage or complaining about the file. The fix is blunt but effective:
# Nuke and rebuild from the in-memory history of your current sessionhistory -a /tmp/history_rescue.txtmv ~/.bash_history ~/.bash_history.bakmv /tmp/history_rescue.txt ~/.bash_historyYou lose history from other sessions, but at least it’s clean again. Your 2 AM self will not care about the loss.