Prompt_command to reload from .bash_history

9

3

My .bashrc contains the following:

HISTSIZE=30000
PROMPT_COMMAND="history -a"
export HISTSIZE PROMPT_COMMAND

What I notice:

  • the .bash_history does get appended.
  • history on a given terminal session does not include entries appended from other terminal sessions

The desired behavior is to have the .bash_history reloaded after any command on any terminal. What is the way to achieve this? Presumably it would be a modification to the PROMPT_COMMAND ?

javadba

Posted 2016-12-21T00:04:33.280

Reputation: 2 201

Answers

14

The history -a command only appends to the history file. This doesn't affect any shell session unless you also read from it. So, what you're looking for is:

PROMPT_COMMAND="history -a; history -r"

As explained in help history:

  -r    read the history file and append the contents to the history
        list

This way, you will first append your current shell's history to $HISTFILE and then read from it, importing the history appended from any other shell instance into the current one.

Note that if you run a command in one terminal, it won't appear in the history of another until you run a command in the second. The $PROMPT_COMMAND is run before a prompt is shown, so if the second terminal is just open and sitting there, it won't read the new command run in the first one until you run something in the second and a new prompt is shown.

terdon

Posted 2016-12-21T00:04:33.280

Reputation: 45 216

I've added history -r now - and that does fix the issue: thanks. Afa "running something new" - the "new" something is history | tail -n 100 so that was being done properly. – javadba – 2016-12-21T16:41:38.243