32

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?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Jack
  • 425
  • 2
    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 – magor May 29 '16 at 19:59

5 Answers5

20

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.

Anthon
  • 79,293
4

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
Vombat
  • 12,884
4

Verifiy that your HISTCONTROL shell 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.

gawi
  • 141
  • 1
    This answer is useful but not accurate. HISTCONTROL is not an environment variable, but a shell variable. On a typical system, "env | grep HISTCONTROL" will not report anything, use "declare -p | grep HISTCONTROL" instead. – ocroquette Dec 07 '23 at 19:28
2

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.

Tycon
  • 41
  • 6
    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
1

Just as the OP, I found loosing history due to multiple terminals, and the 'random order' in which they may exit annoying. I wanted to be able to transfer history from terminal to terminal, but I didn't want it done continuiously, only when needed, or to save it when a shell exits, merging it with other terminals.

This was the result...

Merge the on-disk ".bash_history" with the in-memory shell 'history'. Preserving timestamp ordering, and command order within those timestamps.

Optionally removing unique commands (even if multi-line), and/or removing (cleaning out) simple and/or sensitive commands, according to defined perl RE's. Adjust to suit!

https://antofthy.gitlab.io/software/history_merge.bash.txt

I source the script using an alias 'hc', and from ".bash_logout", but only if I have not turned off history save (unset $HISTFILE, - a sort of 'private' shell session for some situations).

Enjoy.

anthony
  • 610