2

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.

migrated from serverfault.com Apr 14 '15 at 16:05

This question came from our site for system and network administrators.

  • 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 7 '15 at 7:50
  • That is a possible explanation but I think I've seen that happen in two consecutive logins from different locations. – nsn Apr 7 '15 at 7:54
4

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.

  • 1
    And this results in complete insanity if you're like me and have half a dozen terminals open at once... – Michael Hampton Apr 10 '15 at 9:04
  • @MichaelHampton Maybe this question will help you. – tmp Apr 10 '15 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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