Bash – Inconsistent Bash history

bashcommand history

On this Debian Jessie installation, Bash history seems to be behaving inconsistently in Gnome Terminal:

$ echo $USER
me
$ echo $HISTFILE
/home/me/.bash_history
$ grep browser ~/.bash_history 
browser-sync start
$ history | grep browser
 2071  grep browser ~/.bash_history 
 2073  history | grep browser

The browser-sync line from the ~/.bash_history file is quite old: much older than the current Terminal session. Why isn't it showing up in the results from history | grep browser?

Best Answer

By default, ~/.bash_history is written to disk when an interactive session terminates. This means that if you have concurrent shells open, only the most recently terminated shell session has its history written to disk.

(technically, all sessions are written to disk, but two concurrent sessions don't know about the others' histories, so when one exits, it writes what it thinks the new history to be, and then when the other exits, it writes its history)

See https://unix.stackexchange.com/a/1292/20246 for more details.

Related Question