4

I work with Linux for quite some time and have seen this behaviour but I never got a good explanation. Probably it's even very simple.

If I login twice with the same user (bash) in the same server it's possible that I get different results with the history command.

I always thought there was only one .bash_history file and therefore one result.

sendmoreinfo
  • 2,573
nsn
  • 291
  • 1
  • 7
  • 3
    By default bash writes to .bash_history once on exit, so while you works on two bashs simultaneously they will maintain theirs history in memory. – Alexey Ten Apr 07 '15 at 07:50
  • That is a possible explanation but I think I've seen that happen in two consecutive logins from different locations. – nsn Apr 07 '15 at 07:54

1 Answers1

5

History is loaded from file during bash startup. And file is saved automatically when bash exits. During bash execution, history is kept in memory and not synchronized with history file nor multiple bash instances.

You can use history builtin command to manually save your current history to file or to load it from disk (see help history for details).

Only one .bash_history file exists (because two files can't have the same name), but it's possible to set the HISTFILE variable to use other file.

Some useful quotes from man bash (HISTORY section):

On startup, the history is initialized from the file named by the variable HISTFILE (default ~/.bash_history).

and

When a shell with history enabled exits, the last $HISTSIZE lines are copied from the history list to $HISTFILE.

tmp
  • 205