Bash history finally done right

By default bash history is bad at sharing between terminals. I want:

  • Union of all terminals’ history in a new terminal
  • Each terminal to keep its own history while it is open

And finally, via this post and the comment by Jo Liss, I’m happy:

# Insert into .bashrc

# Make sure you remove the existing history lines
# Usually:
###
#    HISTCONTROL=ignoreboth
#    shopt -s histappend
#    HISTSIZE=1000
#    HISTFILESIZE=2000
###

HISTSIZE=9000
HISTFILESIZE=$HISTSIZE
HISTCONTROL=ignorespace:ignoredups
_bash_history_sync() {
    builtin history -a
    HISTFILESIZE=$HISTSIZE
}
history() {
    _bash_history_sync
    builtin history "$@"
}
PROMPT_COMMAND=_bash_history_sync