Bash – sometimes history commands are not stored in .bash_history

bashcommand history

I issued many commands yesterday in my CentOS 7. But when I wanted to retrieve these commands today, I found there was no any record. When I opened the file .bash_history, I still could not find the commands I issued yesterday but I found many old commands a few days ago. Why were the recent commands not stored? How can I increase the history capability?

Best Answer

The most likely cause for history items not to show up is not by setting HISTFILE to nothing or HISTSIZE to zero, it is by logging into the same machine twice and exiting with the second bash instance (in which you did little or nothing) after the one where you did a lot. By default Bash doesn't merge histories and the second Bash-exit overwrites the .bash_history that was so nicely update by the first Bash-exit.

To prevent this from happening you can append to the history file instead of overwriting, you can use the histappend shell option:

If the histappend shell option is enabled (see the description of shopt under SHELL BUILTIN COMMANDS below), the lines are appended to the history file, otherwise the history file is overwritten.

More details in this answer including how to use HISTSIZE, HISTFILESIZE and HISTCONTROL to control size, duplicates etc.