sometimes history commands are not stored in .bash_history

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.


To modify history size use two BASH variables HISTSIZE, HISTFILESIZE (usually set in .bashrc).

Description from BASH man page:

HISTSIZE The number of commands to remember in the command history (see HISTORY below). 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.

HISTFILESIZE The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is trun‐ cated, if necessary, to contain no more than that number of lines by removing the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files.

As an example I have the following setup in my .bashrc file:

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

Verifiy that your HISTCONTROL environment variable is not set to ignorespace or ignoreboth.

$ echo $HISTCONTROL
ignoredups  #this is ok

You can modify it by setting the value in your ~/.bash_profile or your ~/.profile file:

HISTCONTROL=ignoredups

From bash man page:

HISTCONTROL

A colon-separated list of values controlling how commands are saved on the history list.

If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list.

A value of ignoredups causes lines matching the previous history entry to not be saved.

A value of ignoreboth is shorthand for ignorespace and ignoredups.

A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved.

Any value not in the above list is ignored.

If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.