15

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?

  • Sometimes when the terminal is closed abnormally, history is not saved, it happened to me as well but I don't know the circumstances which are causing this behavior – mazs May 29 '16 at 19:59
9

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.

3

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
1

I had this happen but the solution wasn't listed here. The cause was just a simple copy+paste issue. I was copying a command from another document and I had some extra white space on the clipboard. For some reason this caused Ubuntu to not save the command to the history file.

enter image description here

The above image was the command I had in my clipboard form google docs. This command ran but was omitted form my history. I just went back and selected the command with no whitespace and then it appeared in the bash history.

  • 4
    One of the history options is to ignore (don't save) lines that start with a space. It's usually defaulted to on. This can be changed in your .bashrc. – studog Nov 17 '17 at 14:34
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.