4

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?

2 Answers2

5

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.

DopeGhoti
  • 76,081
  • 1
    Thanks for the suggestion, but this answer does not actually answer my question. Based on @TomHunt's comment above, I was able to identify a fix for the issue: see my answer. –  Dec 01 '15 at 18:24
4

The $HISTSIZE variable has too low a value. The apparent inconsistency can be resolved by sufficiently increasing the value of $HISTSIZE.

The Bash manual doesn't explain it very well:

HISTSIZE

The maximum number of commands to remember on the history list. If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.

Here is a better explanation:

The default value for HISTSIZE (500) would load only a fraction of the saved history [if the saved history is greater than 500 lines].