bash (or zsh) HISTSIZE vs. HISTFILESIZE?

Short answer:

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.

(NOTE: as noted by this, in zsh HISTFILESIZE should be SAVEHIST)

Long answer:

All the info above + some examples:

Example 1:
HISTFILESIZE=10 and HISTSIZE=10

  1. You start your session.
  2. Your HISTFILE (file that stores your bash command history), is truncated to contain HISTFILESIZE=10 lines.
  3. You write 50 lines.
  4. At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
  5. You end your session.
  6. Assuming 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.
  7. Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
  8. You now have 10 commands in your history – the last 10 that you just typed in the session you just finished.
  9. When you start a new session, you start over at step 1 with a HISTFILE of HISTFILESIZE=10.

Example 2:
HISTFILESIZE=10 and HISTSIZE=5

  1. You start your session.
  2. Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=10 lines.
  3. You write 50 lines.
  4. At the end of your 50 commands, only commands 46 to 50 are in your history list, whose size is determined by HISTSIZE=5.
  5. You end your session.
  6. Assuming 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.
  7. Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
  8. You now have 10 commands in your history – 5 from a previous session and the last 5 that you just typed in the session you just finished.
  9. When you start a new session, you start over at step 1 with a HISTFILE of HISTFILESIZE=10.

Example 3:
HISTFILESIZE=5 and HISTSIZE=10

  1. You start your session.
  2. Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=5 lines.
  3. You write 50 lines.
  4. At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
  5. You end your session.
  6. Assuming 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.
  7. Your HISTFILE is truncated to contain HISTFILESIZE=5 lines.
  8. You now have 5 commands in your history – the last 5 that you just typed in the session you just finished.
  9. When you start a new session, you start over at step 1 with a HISTFILE of HISTFILESIZE=5.

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 up to
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.

Leave a Comment