HISTFILESIZE is how many commands can be stored in the . bash_history file. HISTSIZE is the number of cached commands.
HISTFILESIZE is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.
Increasing size of Bash History Bash by default keeps 500 commands in the history list. However, we can change this value using the HISTSIZE value. Similarly, the default size of the bash history file is 500. It is the maximum number of entries that are contained in the history file.
$HISTFILESIZE This command helps to set the number of commands kept in your history file. Syntax: HISTFILESIZE=<number>
HISTSIZE
is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.
HISTFILESIZE
is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.
Notice the distinction between file
: on disk - and list
: in memory.
All the info above + some examples:
Example 1:
HISTFILESIZE=10
and HISTSIZE=10
histappend
is not enabled, commands 41 to 50 are saved to your HISTFILE which now has the 10 commands it held at the beginning plus the 10 newly written commands.Example 2:
HISTFILESIZE=10
and HISTSIZE=5
histappend
is not enabled, commands 46 to 50 are saved to your HISTFILE which now has the 10 commands it held at the beginning plus the 5 newly written commands. Example 3:
HISTFILESIZE=5
and HISTSIZE=10
histappend
is not enabled, commands 41 to 50 are saved to your HISTFILE which now has the 5 commands it held at the beginning plus the 10 newly written commands. Info from elixir_sinari:
The history "file" is not updated as you type the commands. The commands get stored in a "list" separately (accessed by the history command). The number of these stored commands is controlled by HISTSIZE value. When the shell (interactive) exits, the last $HISTSIZE lines are copied/appended to $HISTFILE from that "list". If HISTFILESIZE is set, then after this operation, it is ensured that only $HISTFILESIZE lines (latest) exist in $HISTFILE . And when the shell starts, the "list" is initialized from $HISTFILE upto a maximum of $HISTSIZE commands.
And from the man bash
page:
The value of the HISTSIZE variable is used as the number of commands to save in a history list. The text of the last HISTSIZE commands (default 500) is saved. (...)
On startup, the history is initialized from the file named by the variable HISTFILE (default ~/.bash_history). The file named by the value of HISTFILE is truncated, if necessary, to contain no more than the number of lines specified by the value of HISTFILESIZE. (...) When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to $HISTFILE. 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. If HISTFILE is unset, or if the history file is unwritable, the history is not saved. (...) After saving the history, the history file is truncated to contain no more than HISTFILESIZE lines. If HISTFILESIZE is not set, no truncation is performed.
Building on top of what arturomp have said and in an effort to make it a bit clearer.
Assumming you have 2000-something long history..
~$ history
1 sdf
2 fghdfgjf
3 fghfghdf
.. ..
2027 78
2028 57
2029 yu45u
You can cut down what you are shown with HISTSIZE
~$ HISTSIZE=5
~$ history
2026 546
2027 78
2028 56
2029 yu45u
2030 HISTSIZE=5
Now, no matter how many commands you type, only the last 5 will be recorded.
~$ ABC
~$ GGH
~$ GSDHFG
~$ JFDR
~$ ABSDDS
~$ AHFGHFD
<close terminal>
<open new terminal>
~$ history
1 sdf
2 fghdfgjf
3 fghfghdf
.. ..
2028 56
2029 yu45u
2030 HISTSIZE=5
2031 GGH
2032 GSDHFG
2033 JFDR
2034 ABSDDS
2035 AHFGHFD
We can clearly see that our first command ("ABC") is not in the history since only the last 5 commands were recorded.
Now, the total history is stored in a file (.bash_history
) and you can alter how long this file gets with the HISTFILESIZE
. For example with a 2033
HISTFILESIZE
, in my case I would have this:
~$ history
1 fghfghdf
2 gegege
3 gege
.. ..
2028 HISTSIZE=5
2029 GGH
2030 GSDHFG
2031 JFDR
2032 ABSDDS
2033 AHFGHFD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With