How to sync terminal session command history in bash?

Add this line to .bashrc:

export PROMPT_COMMAND="history -a; history -n"

Open new terminal and check.

Explanation

  • history -a appends new history lines to history file.
  • history -n tells bash to read lines that is not read from history file to current history list of session.
  • PROMPT_COMMAND: contents of this variable is run as regular command before bash show prompt. So every time after you execute a command, history -a; history -n is executed, and your bash history is synced.