1. Overview
In this tutorial, we’ll understand the significance of the Bash HISTFILESIZE and HISTSIZE variables. We’ll also check their default values and how they differ.
2. Bash History and Its Default Behavior
When the Bash shell is loaded interactively on user login, it reads the contents of the history file into memory. During the shell session, it adds content to the in-memory copy of the history.
When we execute the history command on a terminal, Bash reads the in-memory copy to show the history of executed commands. Finally, when the shell exits, it writes the in-memory content back to disk on the file pointed by the HISTFILE environment variable.
By default, the contents are stored in the .bash_history file located at the user’s home directory:
$ echo $HISTFILE
/home/shubh/.bash_history
The number of lines that can be stored in the history file is governed by two special variables HISTSIZE and HISTFILESIZE.
3. HISTSIZE and HISTFILESIZE Variables
The value set in the HISTSIZE variable is the maximum number of lines of history that we can store in memory. Let’s start by checking the default value of the variable:
$ echo $HISTSIZE
500
This means that by default, we can store a maximum of 500 lines in history for a given Bash session.
To better understand the concepts, let’s experiment a bit:
$ HISTSIZE=3
$ echo $HISTSIZE
3
We’ve set the variable to hold only 3 records in memory. Let’s verify by executing the history command:
$ history
1502 HISTSIZE=3
1503 echo $HISTSIZE
1504 history
Let’s further check the behavior by executing some more commands:
$ echo "Let's add a record to history"
Let's add a record to history
$ history
2511 history
2512 echo "Let's add a record to history"
2513 history
As we can see, we can store only the last 3 commands in the Bash memory.
On the other hand, the HISTFILESIZE variable controls the maximum number of lines that we can write back to the history file on disk.
Let’s check the default value of the variable:
$ echo $HISTFILESIZE
500
Hence by default, we can write a maximum of 500 lines to the ~/.bash_history file.
Consider a situation in which we execute more than 500 commands in a Bash session. Hence on Bash exit, only the last 500 commands are stored back in the history file when the Bash session ends.
4. Understanding the Differences
We’ll now experiment a bit and set the value of HISTFILESIZE smaller than the value of the HISTSIZE:
$ HISTFILESIZE=5 && echo $HISTFILESIZE
5
$ HISTSIZE=10 && echo $HISTSIZE
10
Now, let’s execute some test commands on the terminal:
$ echo "Command1" >/dev/null
$ echo "Command2" >/dev/null
$ echo "Command3" >/dev/null
$ echo "Command4" >/dev/null
$ echo "Command5" >/dev/null
$ echo "Command6" >/dev/null
$ echo "Command7" >/dev/null
$ echo "Command8" >/dev/null
$ echo "Command9" >/dev/null
$ echo "Command10" >/dev/null
$ echo "Command11" >/dev/null
$ echo "Command12" >/dev/null
Let’s check the output of the history command:
$ history
508 echo "Command4" >/dev/null
509 echo "Command5" >/dev/null
510 echo "Command6" >/dev/null
511 echo "Command7" >/dev/null
512 echo "Command8" >/dev/null
513 echo "Command9" >/dev/null
514 echo "Command10" >/dev/null
515 echo "Command11" >/dev/null
516 echo "Command12" >/dev/null
517 history
As we can observe, the output shows the last 10 commands. This is equal to the value of the HISTSIZE variable.
Let’s now exit gracefully from the current Bash session, re-login again, and verify the output of the history command:
$ history
1 echo "Command10" >/dev/null
2 echo "Command11" >/dev/null
3 echo "Command12" >/dev/null
4 history
5 exit
Here, the output contains only the last 5 lines of the previous session. As this example shows, only the last n lines, with n equal to the value of HISTFILESIZE, are written to the disk and are available to the next sessions.
It is important to note we may lose history data due to incompatible values in these environment variables. Hence, we should always choose their values carefully. Those values should be large enough to accommodate the session and overall history.
4. Conclusion
In this tutorial, we discussed the importance of Bash history variables HISTSIZE and HISTFILESIZE, along with their differences.