16

How is command line history stored when I use multiple terminal windows? I know it is stored in .bash_history but I can't see the logic on what history is used if I open new window. It almost feels nondeterministic in a sense that I never know what command I will see if I try to use up arrow in new window.

Can someone explain this?

Is there a way to control history in such way that I can reuse history from particular window?

3 Answers 3

16

To understand the behaviour of the bash history first you have to know the following:

  1. There is the history in the history file.
  2. There is the history in the memory of a bash process.
  3. The history in the memory of one bash process is not synced with the history in the memory of any other bash process.
  4. The history in the memory of a bash process is not synced with the history in the file, unless explicitly asked to or during some specific event (see below).

Using the default settings, the lifecycle of a bash session with regard to the history is as follows:

  1. During startup bash will read the history file. The content of the history file is now in the memory of the bash process.
  2. During normal use only the history in memory is manipulated.
  3. During shutdown the history in memory is written to the history file, overwriting any previous content of the history file.

The seemingly nondeterministic behaviour you have observed is mostly because the content of the history file is always the history of the last closed bash session, and bash only reads the history file during startup.

Read the bash manual for a more detailed explanation of the startup and shutdown process.

Note that with default settings I mean the default settings from bash. Your distribution might have provided a .bashrc (or /etc/bash.bashrc) which change this behaviour.

By enabling the shell option histappend you can tell bash to append instead of overwriting the history file. You can enable histappend using the command shopt -s histappend. To have this option always enabled you have to put the command in your .bashrc (or other initialization file). Read more about the shopt command in the bash manual

Note that enabling histappend will not reduce the seemingly nondeterministic behaviour by much. This is because every bash session still has it's own history in memory. It is possible to have a mostly synchronized bash history. There is a guide how to get every bash process to have a mostly synced history in a thread on stack overflow.

using the builtin command history you can explicitly tell bash to read the history from file to memory, or the write from memory to file. For example: history -r will read the content of the file and append it to the history in memory (this mimics bash behaviour during start). history -w will write the current history from memory to file, overwriting the previous content (this mimics bash behaviour during shutdown). Read more about the history command in the bash manual

For completeness here is a list of the internal variables which modify the history behaviour:

  • HISTFILE: the file to read from and write the history to.
  • HISTCONTROL, HISTIGNORE: prevent some commands from being saved to history (applies only to in memory history)
  • HISTFILESIZE, HISTSIZE, HISTTIMEFORMAT: not relevant for this discussion.

Read the bash manual for details.

4
  • Good explanation. You mentioned shutdown, but what about killing terminal session? Session can be killed via logout or via UI or via some other means like drop in network connection. If entire history file is replaced and you have multiple sessions, are you saying that history from last closed one will be used in history file? That could explain non deterministic behavior. Dec 2, 2011 at 19:47
  • lifecycle point (3) is not correct. It seems like only the first bash session will ever write to the history file. Test: Open 2 sessions in order- a,b. Do 'echo hello' in b. Then exit in b. Then open a new session c. This session will not have echo hello in it's history.
    – user606723
    Dec 2, 2011 at 19:56
  • @AlexGitelman: if a bash process is killed then it will not have the chance to overwrite the history file. and yes, the history from the last closed session is the one that will be in the history file.
    – Lesmana
    Dec 3, 2011 at 8:05
  • @user606723: point 3 is correct. read the bash manual. experiment again using a minimal .bashrc file. note that your distribution might have changed some settings in /etc/bash.bashrc. check specifically for the shell option histappend.
    – Lesmana
    Dec 3, 2011 at 8:08
2

http://www.gnu.org/software/bash/manual/bashref.html#Using-History-Interactively

You might be able to manipulate how the history file is written to with one of the terminals, i.e., execute "history -a" or "history -w" in the terminal you want to save history, and then "history -r" in the other terminals. Depends on what you want to do.

0

AFAIK, the bash commands are saved after the SSH session is terminated. So, the commands are not saved when a session terminates abnormally (for example, due to network failure). I am talking here about SSH sessions. The local terminals may use similar approach.

When opening multiple sessions at the same time, the commands typed on one session are not seen on the other while they are both active. However, you will see these commands when you terminate your session re-open it.

5
  • This is not the behavior I've experienced. I can confirm this by doing a quick test where I open an ssh session, do a command, and do a graceful exit. In this case, i had another previously active ssh session. In this pre-existing bash session, I check .bash_history and find nothing recorded. I find it likely that the first bash session running is the only one that ends up recording to .bash_history.
    – user606723
    Dec 2, 2011 at 19:02
  • Terminating your session will not affect the currently running sessions, but it will affect the new sessions!
    – Khaled
    Dec 2, 2011 at 19:06
  • What if it is not SSH but Gnome terminal window that I close via UI? Dec 2, 2011 at 19:33
  • It needs to be verified. Currently, I don't have access to a Gnome terminal!
    – Khaled
    Dec 2, 2011 at 19:36
  • @Khaled, Tested it, does not affect new sessions. (I checked .bash_history before anyways, which is where bash gets the command history for new sessions, I so knew it wouldn;t work, but I humour'ed you anyway.)
    – user606723
    Dec 2, 2011 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .