Use a common bash command history for all bash sessions

By default bash is configured with each bash shell having it’s own command history. I believe it’s associated with the shell’s pseudo tty if I’m not mistaken. I find this to be a poor default configuration. 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.

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.

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

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.

Here’s the relevant stuff from my .bashrc

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.