Why isn’t `history -a` working

bashhistoryterminal

I am working under OS X 10.9.1 (Mavericks), using Terminal, which reports that it is GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)

I noticed some weird issues where history was not working properly: history -a and history -a filename.txt don't do anything. history shows the history I would expect. This breaks my .bashrc synchronization of history across terminals.

I started to investigate by manually changing bash HIST variables. Here is how I am able to reproduce my problem under somewhat controlled conditions:

  1. I disabled my .bashrc
  2. I opened a new terminal window and cleared the history

    history -c
    
  3. I confirmed that my PROMPT_COMMAND is back to normal/default:

    echo $PROMPT_COMMAND
    

    This returns: update_terminal_cwd;

  4. I manually set the relevant HIST variables:

    export HISTFILE="/Users/rsage/temp_history.txt"
    export HISTSIZE=20000
    export HISTFILESIZE=20000
    
  5. I confirm my test history files are deleted:

    ls *history*.txt # To make sure I know what I'm about to delete
    rm *history*.txt
    
  6. I attempt to save the history using history -a with no luck

    history -a
    history -a history_a.txt
    ls -ltr ; date
    

    This latter shows no history files:

    ...
    drwx------+ 40 rsage  staff   1360 Dec 20 14:16 Desktop
    drwxr-xr-x   4 rsage  staff    136 Dec 20 18:48 webApps
    drwxr-xr-x   8 rsage  staff    272 Dec 22 09:11 code
    drwxr-xr-x  17 rsage  staff    578 Dec 22 09:26 stuff
    

    Sun Dec 22 10:17:50 PST 2013

  7. …but I stumbled across the history -w command (I know it overwrites the file, which is fine with my nominal bashrc) and this seems to work fine:

    history -w
    history -w history_w.txt
    ls -ltr ; date
    

    which produces the expected results:

    ...
    drwx------+ 40 rsage  staff   1360 Dec 20 14:16 Desktop
    drwxr-xr-x   4 rsage  staff    136 Dec 20 18:48 webApps
    drwxr-xr-x   8 rsage  staff    272 Dec 22 09:11 code
    drwxr-xr-x  17 rsage  staff    578 Dec 22 09:26 stuff
    -rw-------   1 rsage  staff    461 Dec 22 10:19 temp_history.txt
    -rw-------   1 rsage  staff    494 Dec 22 10:19 history_w.txt
    

One last note. It occurred to me that my sizes might be too large, so I just tried 200 and no change to behavior (the thirties are the history output numbers):

34  export HISTFILESIZE=200
35  export HISTSIZE=200
36  history -a
37  history -a history_a.txt
38  ls -ltr ; date

The ls outputs show no new files.

Best Answer

I noticed the same odd behavior, checked permissions, and sure enough, both the "World" and "System" had explicit permissions set to read AND write my .bash_history file, but I (the owner) had no permissions to that file at all!

I just forced "World" to "No Access" and gave myself and system r/w access and voila, everything is working the way it should.