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?