Syncing bash history across sessions

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

I always work with multiple sessions and have from 5-10 sessions. I work on one session and then switch to another session, I try to do Ctrl + R , however since the bash history are not synced, I need to go to that session and then copy the command.
The more frustrating encounter is when you remember the keywords of the statement but used it long back. You will surely lose the history, being lots of sessions un-synced.

The above command provides just that – sync bash history from multiple sessions of bash.

  • PROMPT_COMMAND when a new command is executed, commands in this variable is executed. Commands in this variable is executed every time bash prompt is shown.
  • history -a appends new command statement to bash history file
  • history -n statement to read any unread lines from bash history file to current session

Leave a Reply

Your email address will not be published.