Problem statement
I've been following numerous examples for doing creative things with bash history along the lines of:
# simplified example
PROMPT_COMMAND='history -a; history -c; history -r'
... but it seemed to work intermittently. If the history file already exists with previous history then everything is fine.
Other relevant info
- bash versions: 3.2.51, 4.1.2, and 4.2.45
- 4.2.x doesn't appear to have this problem, but it's a non-default install
- OS: Linux & Solaris
- home dir is NFS mounted
Diagnostic steps
I got rid of my .bashrc
& set my .bash_profile
to:
HISTFILE=$HOME/.bash_history.test
I would then start a login shell (eg, ssh to another machine) & do something like the following:
~ cat .bash_history.test
cat: .bash_history.test: No such file or directory
~ history
1 cat .bash_history.test
2 history
~ history -a
~ !-3
cat .bash_history.test
cat: .bash_history.test: No such file or directory
The history file won't get appended to by history -a
under the following circumstances:
- The file doesn't already exist
- The file is empty or only contains newlines; even whitespace will make it work
... however, when the shell exits it does create it. Thereafter, history -a
works as expected ... except when PROMPT_COMMAND="history -a; history -c; history -r"
. When I had that set, even exiting the shell didn't create the history file (unless I ran exec bash
first).
I then tried with a non-standard install of bash (4.2.x) and the problem didn't manifest.