Skip to content
Go back

Preserving Bash History in Multiple Terminal Windows

· Updated:
By SumGuy 6 min read
Preserving Bash History in Multiple Terminal Windows

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:

nano ~/.bashrc
# 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:

source ~/.bashrc

Pros and Cons of Sharing Bash History

Pros:

Cons:

Tips for Managing Bash History

export HISTSIZE=10000
export HISTFILESIZE=20000

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:

Terminal window
# WRONG — blows away anything else that set PROMPT_COMMAND
export PROMPT_COMMAND="history -a; history -c; history -r"
# RIGHT — safe to chain with other hooks
export 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:

Terminal window
# Reload history config in the current pane only
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND:-}"
shopt -s histappend

Alias 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:

Terminal window
# Nuke and rebuild from the in-memory history of your current session
history -a /tmp/history_rescue.txt
mv ~/.bash_history ~/.bash_history.bak
mv /tmp/history_rescue.txt ~/.bash_history

You lose history from other sessions, but at least it’s clean again. Your 2 AM self will not care about the loss.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Docker Manager Showdown: Pick One
Next Post
Prompt Engineering for Generative AI 101

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts