Mangled history

command history

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?

Best Answer

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