Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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 exits
  • incappendhistory -- The master history file is updated each time a line is executed in any shell, instead of waiting until that shell exits
  • sharehistory -- Like incappendhistory, 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
share|improve this answer
    
Now tell me how to do that in Bash please ;) –  tante Oct 15 '10 at 14:07
1  
@tante Does shopt -s histappend not work? –  Michael Mrozek Oct 15 '10 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.

share|improve this answer

tcsh: set savehist = (5000 merge) merges

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.