Bash history following new terminal

Currently I am using Fedora 32 Mate spin. I usually go between using MATE Terminal and Terminator. But recently when I go to use either and have multiple tabs open if I back out of an ssh session and later want to reconnect I hit the up arrow and I get the last command from another terminal tab. I feel like its something stupid that I’ve overlooked in my .bashrc.

I have the following in my .bashrc

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)

But I also have the following:

# After each command, append to the history file and reread it
_bash_history_sync() {
  builtin history -a         #1
  builtin history -c         #3
  builtin history -r         #4

history() {                  #5
  builtin history "$@"


Sometimes you need to manually terminate the previous session when reconnecting SSH:

loginctl list-sessions 
loginctl terminate-session SESSION_ID

So I should stated that I am not remote on this machine. But usually 90% in-front of it when this happens. The SSH sessions are outbound only.

1 Like

Probably you shouldn’t perform the history sync implicitly.
In particular the clear/read part which affects the runtime history:

So comment those out?

Yes, otherwise it leads to a race condition.

If you want to append new history lines from other tabs to the current tab, you can run explicitly:

history -n