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 Answers3

16

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

  1. There is a history file.
  2. There is a history in a running bash session.
  3. Multiple bash session each have their own history and do not sync with each other.
  4. The session history is not synced with the history 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 the bash session history.
  2. During normal use only the session history is manipulated.
  3. During shutdown the history file is overwritten with the session history. All previous content of the history file is gone.

Read the bash manual for a more detailed explanation of the startup and shutdown process: http://www.gnu.org/software/bash/manual/html_node/Bash-History-Facilities.html#Bash-History-Facilities

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

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: http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html#The-Shopt-Builtin

Note that enabling histappend will not reduce the seemingly nondeterministic behaviour by much. This is because every bash session still has it's own session history.

using the builtin command history you can explicitly tell bash to read from the history file to session history or the write the session history to history file.

For example: history -r will read the content of the file and append it to the session history (this mimics bash behaviour during start). history -w will write the session history to history file, overwriting the previous content (this mimics bash behaviour during shutdown). Read more about the history command in the bash manual: http://www.gnu.org/software/bash/manual/html_node/Bash-History-Builtins.html#Bash-History-Builtins

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 session history. not history file.

Read the bash manual for more: http://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#Bash-Variables

With a bit of hacking it is possible to have a mostly synchronized bash history. There is a discussion how to get every bash process to have a mostly synced history in stack overflow: https://stackoverflow.com/questions/103944/real-time-history-export-amongst-bash-terminal-windows/3055135#3055135

Lesmana
  • 2,324
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.

cjc
  • 25,492
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.

Khaled
  • 37,789