6

Today upon bringing up a bash terminal I tried finding a frequently-used command from my bash history via Ctrl-r. To my surprise, it wasn't found. Upon inspecting ~/.bash_history, I saw instances of the command on several lines. Does the command history not work by checking the file, either in real time or against a cached copy?

2 Answers 2

7

It turns out there was a little more nuance than I was aware of: HISTFILESIZE determines the maximum number of lines saved in .bash_history, while HISTSIZE determines the number of lines from ~/.bash_history loaded into a searchable cache. Upon increasing HISTSIZE to match HISTFILESIZE, I was able to find the command via Ctrl-r.

2
  • Thanks for posting this Q&A. I hope this clear demonstration of the interaction of these variables will help others too. Sep 22, 2020 at 15:05
  • It actually started as just a Q, but a few more minutes of digging yielded an answer! Sep 22, 2020 at 15:08
2

Apart from this (HISTFILESIZE), there is one reason why a line won't appear in history: A leading space. If you start a command with a space in the front like this:

$ ls -l

$ ls -l (extra space at the beginning)

then the latter won't show up in history. Related, here is what my .bashrc looks like:

.bashrc:HISTCONTROL=ignoreboth
.bashrc:export HISTSIZE=100000
.bashrc:export HISTFILESIZE=100000
.bashrc:export HISTCONTROL=ignoredups:erasedups
.bashrc:export HISTTIMEFORMAT="%d/%m/%y %T "

HTH

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .