On Sun, 5 Jan 2014, Erik Anderson wrote:

> I'm curious to hear about *why* you are separating your HISTFILEs, 
> though. My guess is that you have a set of different ssh session 
> "types", and you want to be able to isolate history entries for each 
> function. Is that correct?


I'll have maybe 10 connections open to the server at once and each of them 
will be for my work on some project.  So every connection is of its own 
type -- there is no sort of classification scheme, if that's what you were 
asking.

Every time the command prompt returns, it writes the previous command to 
the HISTFILE:

export PROMPT_COMMAND='history -a'

So I have to use separate HISTFILEs or else the commands from different 
projects will be interlaced.  If I don't write every command immediately 
to a histfile, when the sessions are killed by power failure or network 
outage, I'll lose all the command histories.

I have ways to work around the tty issue using history commands, copying 
files, etc.  But I can also get the tty I want, if it is unused, by 
occupying the lower /dev/pts/ numbers.

This is working great for me, and I would recommend it strongly to others. 
I'm sharing the relevant lines from my .bashrc below.  It would be great 
if anyone has anything to add or to correct.  Thanks.

Mike



# Use multi_history?  Change to "yes" if you will often have multiple
# interactive bash shells running simultaneously on this system.  This
# will cause you to save multiple history files, one per shell -- see
# HISTFILE info below.
multi_history=yes


##############################################
#
# HISTORY settings
#
##############################################

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


# If $multi_history=yes, then tty is used to create a different
# $HISTFILE for every tty.  This will be a big advantage for people
# who have multiple interactive bash shells running simultaneously.
# It is not recommended for people who only run one at a time.

# if requested, add the tty to the name of the history file
if [ "$multi_history" = "yes" ]; then
    export HISTFILE=~/.bash_history$(tty | sed 's|/|_|g')
    if [ ! -s $HISTFILE ] ; then
       if [ -s ~/.bash_history_init ] ; then
          cp -fp ~/.bash_history_init $HISTFILE
       else
          echo -e "#1\ncd" >> ~/.bash_history_init
          chmod 600 ~/.bash_history_init
          cp -fp ~/.bash_history_init $HISTFILE
       fi
    fi
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 (recommendation: do not
# add "cd" to this list because doing so makes it hard to track the
# default directory where commands were executed)
HISTIGNORE='ls:laf:jobs:bg:fg'

# 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
export HISTTIMEFORMAT="%F %T%t"



##############################################
#
# Prompt settings
#
##############################################

# somone wrote, "color prompt is turned off by default to not distract
# the user: the focus in a terminal window should be on the output of
# commands, not on the prompt"
# Mike Miller disagrees -- when looking at the scrollback in the
# command window, the focus often is on the prompt because the prompt
# shows where the commands are.
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
       # We have color support; assume it's compliant with Ecma-48
       # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
       # a case would tend to support setf rather than setaf.)
       color_prompt=yes
    else
       color_prompt=no
    fi
fi

if [ "$color_prompt" = yes ]; then
    if [ "$multi_history" = "yes" ]; then
       # add tty info to prompt for multi_history
       PS1="\[\e]0;\u@\h : $(tty) : \w\a\]\n\[\e[32m\]\u@\h\[\e[34m\]:\[\e[36m\]$(tty) \[\e[33m\]\w\[\e[0m\]\n\$ "
    else
       PS1='\[\e]0;\u@\h: \w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
    fi
else
    if [ "$multi_history" = "yes" ]; then
       # add tty info to prompt for multi_history
       PS1='\n\u@\h $(tty) \w\n\$ '
    else
       PS1='\n\u@\h \w\n\$ '
    fi
fi
unset color_prompt force_color_prompt