2

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.

1 Answer 1

3

This mailing list thread appears to explain away these problems; quoting from the 2nd msg:

The current code in bashhist.c:maybe_append_history() (which has existed for at least 15 years) seems to not handle the case where the number of history lines in the current session is equal to the number of history list entries. That is, the code won't append new lines if it doesn't think there were any lines read from the history file when the shell started. That seems wrong, and I've attached a patch that fixes it. However, the code has existed in its current form for so long that I'm wondering whether or not there is some other reason for it.

(Chet Ramey, GNU bash mailing list)

Furthermore, the changelog for 4.2 has the following text:

+                  8/13
+                  ----
+bashhist.c
+   - in maybe_append_history, change check for history_lines_this_session
+     so that we append the lines to the file if it's equal to the value
+     returned by where_history().  This means that without this change,
+     the history won't be appended if all the lines in the history list
+     were added in the current session since the last time the history
+     file was read or written.  Fixes bug reported by Bruce Korb
+     <[email protected]>

(git.savannah.gnu.org, bash git repo)

Obviously we need to get a newer version of bash installed to resolve this problem.

Hopefully this helps someone else down the road.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .