When I started writing this message, it had this subject...

"history -a" not working with custom $HISTFILE

...and I did not have a good solution to the problem, but it seems that I 
have one now.  I think the problem was a bug in Bash that may have been 
fixed some years ago, but on some systems (that I don't manage), I'm still 
using an old Bash, for example:

GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

I think some of you will be interested in my results, especially the 
section below labeled "WHAT I'M DOING NOW".

I want my Bash history to be saved continually to $HISTFILE.  This page 
seems to give fairly standard advice:

http://blog.sanctum.geek.nz/better-bash-history/

I usually have a few shells open at once working on various jobs, so to 
avoid interleaving the command histories, I define $HISTFILE in my 
.bashrc so that the filename depends on the tty:

export HISTFILE=~/.bash_history$(tty | sed 's|/|_|g')

That works to make the desired $HISTFILE.  Example:

$ tty
/dev/pts/9

$ echo $HISTFILE
/home/mbmiller/.bash_history_dev_pts_9

However, the $PROMPT_COMMAND, which is specified in the .bashrc file in 
the following ways (I tried it both ways)...

export PROMPT_COMMAND='history -a'
export PROMPT_COMMAND="history -a;$PROMPT_COMMAND"

...is not working.  It should add every new command to the $HISTFILE 
immediately after it is executed.  Strangely, the failure of 
$PROMPT_COMMAND is no surprise given that neither of these commands add a 
line to $HISTFILE:

$ history -a
$ history -a $HISTFILE

It turns out that this does not work when $HISTFILE is empty.  I guess 
that's a bash bug.  I found this info:

http://lists.gnu.org/archive/html/bug-bash/2008-05/msg00045.html



WHAT I'M DOING NOW

I have the lines below in my ~/.bashrc file.  The if/then statement deals 
with the bash bug in a good way by creating a single cd command with a 
date stamp 1 second into the epoch (so 12/31/1969 in our time zone). 
That creates one history line and everything works after that.

One thing I'm likely to change:  Instead of using "echo" to write initial 
history commands, I'll probably make a file with a list of commands I'll 
likely want to use and call that ~/.bash_history_init, then do this 
instead of what is below:

if [ ! -s $HISTFILE ] ; then cp -fp ~/.bash_history_init $HISTFILE ; fi


# append to the history file, don't overwrite it
shopt -s histappend

# add the tty to the name of the history file
export HISTFILE=~/.bash_history$(tty | sed 's|/|_|g')
if [ ! -s $HISTFILE ] ; then echo -e "#1\ncd" >> $HISTFILE ; chmod 600 $HISTFILE ; fi

# immediately write every new command to the history file
export PROMPT_COMMAND='history -a'

# don't put duplicate lines in the history nor lines beginning with a space
export HISTCONTROL=ignoreboth

# For setting history length see HISTSIZE and HISTFILESIZE in bash(1)
# Save 10,000 lines of history but 100,000 lines in the history file:
export HISTSIZE=10000
export HISTFILESIZE=100000

# commands to ignore and not add to history
HISTIGNORE='ls:laf:laf | less:jobs:bg:fg:history | less'

# set time format for history file display
# in saved file, it uses seconds since 1/1/1970, but those can be converted
# for viewing using this command (where 1234567890 is the date in seconds):
# date +"%F %T" -d @1234567890  or by using the "history" command.  The %t
# adds a tab character so that history | cut -f2- pumps out a simple list
# of commands with no date stamp
export HISTTIMEFORMAT="%F %T%t"