0
I use a bunch of different aix machines at work and need to share users with co-workers.
To make my life easier but not bother others with my customizations, I have created a .bashrc file that I copy to /tmp/matthewh whenever I have to use a new host.
My .bashrc sets a new history file so my personal command history stays isolated from others using the same machine and user.
# History options
export HISTTIMEFORMAT="%F %T "
export HISTCONTROL=ignorespace
export HISTSIZE=2000
export HISTFILESIZE=2000
export HISTFILE="/tmp/matthewh/$USER.bash_history"
After copying the file over, I connect with ssh user@host -t "bash --rcfile /tmp/matthewh/.bashrc"
.
This works perfectly. The problem occurs when I switch to another user and source the .bashrc file.
$ su - apache
$ . /tmp/matthewh/.bashrc
The problem is that the initial su - apache
loads apache's history from ~/.bash_history and then when I source .bashrc after, it's too late. The history file gets changed correctly but it has already been loaded. I can fix this issue by running history -c && history -r
, so all I need to do is put that in the .bashrc too and everything should be fine, right?
Unfortunately, if I put that in my .bashrc file it breaks things when I log in with ssh
and the -t "bash --rcfile /tmp/matthewh/.bashrc"
option. Since that sources it as a login shell, loading the history is part of the process. This means that the history -r
in the .bashrc loads the history a second time and my working history is twice as big as it should be.
Is there a way for .bashrc to detect if it is being sourced manually or from a login shell so it can only clear and reload the history conditionally?
If I did this wouldn't it reload the history file each time I enter a command? If I have two or more terminals open (gnu screen or tmux) this would cause different panes/windows to cross-contaminate history. E.g. in one window I'm making changes to different htaccess files and restarting httpd, in another window I'm doing db2 queries to check things. If I have PROMPT_COMMAND set this way the history for these sessions will get mixed together and make repeated commands less convenient. – Matthew – 2014-08-13T21:10:27.937
1I think you missed the detail that it begins with
unset PROMPT_COMMAND
. – aecolley – 2014-08-13T21:27:41.930You are correct, this would work. I was worried that the history would not be loaded until a command was entered after logging in or sourcing the .bashrc, but this gets executed immediately when the first prompt is shown. – Matthew – 2014-08-13T21:43:09.323