Use a common bash command history for all bash sessions

Bash command history has a problematic default configuration.

Basically there is a single ~/.bash_history file that each bash shell reads at startup. If you start multiple bash’s then they’ll have identical history to begin with. So far so good.

The first problem is that bash does not write new commands to ~/.bash_history until the process terminates. Next problem is that bash doesn’t reread ~/.bash_history in case other processes have updated it. So, if you have multiple bash sessions going on the same host, last one wins.

This means that every time you login, every time you open a new bash session, you have a command history from an unknown previous bash session. And you have no reliable way to recall commands from days, weeks, months ago. And if you regularly have 2, 3 or more bash shells open and used on the same host, you lose all the history from each shell except for the last one to terminate. This is no good at all.

But this is easily fixed.

By making the following configuration change all of your bash sessions share the same command history file. And by making the size of your command history large you can easily have many months and even years of command history available for recall and use. It allows you to recall commands from long ago. They’re still there in the history. You just C-R to search for them. Once you get used to this you won’t want to live without it.

You type a command in one bash shell, then switch to another bash shell (on the same host) and type history and you’ll see the other command. Etc etc. Any command you type in any bash shell (on same host) is available for recall and execution on any other bash shell. For. The. Win.

** Note, in order to cause bash to re-read ~/.bash_history after it has been updated by some other bash, you must hit return once, if you use my configuration below. That causes bash to re read ~/.bash_history. I easily grew accustomed to typing commands in one bash, switching to another and hitting return once to see the updated ~/.bash_history.

Here’s the relevant stuff from my .bash_profile

shopt -s \
cdspell \
checkhash \
checkwinsize \
histappend

HISTFILE=~/.bash_history
HISTSIZE=99999
HISTFILESIZE=$HISTSIZE
HISTCONTROL=ignorespace:ignoredups

history() {
# bash_history_sync
builtin history "$@"
}

bash_history_sync() {
builtin history -a #1
HISTFILESIZE=$HISTSIZE #2
builtin history -c #3
builtin history -r #4
}

PROMPT_COMMAND=bash_history_sync

alias h="history 100"

So this setup runs the bash_history_sync() function each time the bash prompt is displayed. So, when you run a command in one bash shell, then switch to another bash shell and if you then immediately run “history 100” you won’t see the command cause the prompt hasn’t re-displayed yet. But, run history again and it’ll be there. Or what I do is just hit return once then run history. It works.

Update 2023-03-08: I’ve had my HISTSIZE set to 999999 for many years. The # of lines in my .bash_history has grown to over half a million and it’s 22MB in size. The only problem I’ve encountered is that when I type a bad command in bash, one in which bash responds with “command not found”, there is a significant delay before I get the bash prompt back and am able to type the next command. I found that by limiting the HISTSIZE to 100k or 200k and keeping the size down, this problem doesn’t occur. And, I don’t really need history older than a few years ago. But if I do, it’s in the backup. ;^)