In gui mode, when a user has more than one terminal open, how do the terminals rewrite the history file of that user? the reason I ask is because, it is quite possible that in each of the terminals the user ends up executing different commands. So, does the history file end up saving the commands from all the terminals or only from the first one to be opened? Or is there some other kind of scheme that is employed to tackle this situation?
3 Answers
It depends entirely on how the shell chooses to handle it
bash by default will overwrite the history file with the local history of each shell as it exits, so the last shell to exit wins. The histappend option will cause it to append to the master history instead (shopt -s histappend).
zsh does the same by default, and has a few options for dealing with it:
appendhistory-- The history of each shell is appended to the master history file as the shell exitsincappendhistory-- The master history file is updated each time a line is executed in any shell, instead of waiting until that shell exitssharehistory-- Likeincappendhistory, but also pulls changes from the master history file into all running shells, so you can run a command in one shell and then hit Up in another shell and see it
- Now tell me how to do that in Bash please ;)tante– tante2010-10-15 14:07:30 +00:00Commented Oct 15, 2010 at 14:07
- 1@tante Does
shopt -s histappendnot work?Michael Mrozek– Michael Mrozek2010-10-15 14:18:29 +00:00Commented Oct 15, 2010 at 14:18
I originally got this idea from the O'Reilly "Unix Power Tools" book.
In my .profile I set:
export HISTFILE=$HOME/.sh_hist.$$ Every time my .profile gets read, I get a new history file named with the PID of my session. If I have multiple logins, each login gets a unique history file. Works in ksh and bash.
If you're just opening new terminals in an X session, those usually aren't login shells, but you can configure them to act as login shells. For example, rxvt +ls will start rxvt as a login shell. Check the docs for whatever terminal you're using.
Also, unless you're using a .logout or .bash_logout file (or some other means) to clean up, you'll eventually have a crapload of .sh_hist files.