Use multiple tabs in Linux or OS X without losing your history

Bash, the default shell in Linux and OS X, loses history data after running multiple shells at the same time - for example, using multiple tabs in the terminal. The fix is simple, but not widely known.
The Problem
If you’re not familiar with the problem, open a terminal, and a run a command. Then open a second tab (or another terminal) and run a different command.
Disconnect the first shell, then the second (either hit Ctrl-D, or type exit).
Start a new terminal, and run history. The second shell’s history is there, but the history from the first shell is missing.
Why? Bash, by default, overwrites the history when the shell exits. This means whichever shell exited last overwrites the history file, replacing it with:
- the history from when it was started
- the commands run in its own session
History saved by other terminals while the shell was running is lost.
This is a data loss bug, and a bad default. It’s also annoying when you need to remember a command you previously ran.
The Fix
Add this to the bottom of your .bashrc, which gets run every time you open a new tab (if you’re wondering, .bash_profile only gets run when logging in).
## Append the history rather than overwrite it
shopt -s histappend
## Save the history after each command finishes
## (and keep any existing PROMPT_COMMAND settings)
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
You can read about more about each of these from ‘man bash’.
Now bash will add each command to your history after it finishes and returns to the prompt.
Close all your shells, the repeat the experiment above to test it. When you launch a shell, you’ll always find the full history from every command run so far has been saved.
Simple moderation policy:
- Contribute something
- Justify your opinion
- Be courteous to others


November 8th, 2007 17:58
For anyone wanting to keep any default, or previously defined PROMPT_COMMAND you’d want to use something like this:
PROMPT_COMMAND=”${PROMPT_COMMAND}; history -a;”
This way you can keep the previously set PROMPT_COMMAND, by redefining PROMPT_COMMAND with itself and adding on your desired commands.
Ed:Great suggestion Brett - I’ve added it to the article. Thanks!
November 9th, 2007 01:56
Excellent! You might be interested to know that it also works in Cygwin Bash (not surprisingly).
November 11th, 2007 13:00
great, i am annoyed by this problem for a long time.
November 17th, 2007 03:19
One thing that might be even better would be to be able to save *separate* histories for multiple shells. I typically have as many as 8 different terminal windows open at once. I think it would be great to have separate histories and, even more, separate directory stacks (I use pushd and popd a lot) for each of the terminal windows. Somehow, though, you’d have to be able to have a terminal window “know” its identity, and then when restarted (I use KDE) somehow find the same identity. If you could do that, it would be simple to associate separate histories and directory stacks with separate shell instances.