238

What is the difference in HISTSIZE vs. HISTFILESIZE?

They are used to extend bash history beyond the default 500 lines.

There seems to be a lack of clarity here and in other forums about why they are both needed. (Example 1, Example 2, Example 3).

1
  • It's indeed ridiculous how unclear most blog posts are about this difference, even from redhat. Dec 27, 2021 at 4:00

2 Answers 2

397

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.

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.

12
  • 38
    +1 This answer is impressively thorough! A bit too much though, I think. Most people won't reach the end of it. I think you should consider summarizing it
    – salezica
    Jun 7, 2014 at 0:44
  • 32
    @slezica I disagree with the second part of your comment: I reached the end of it and I can say that every bit of information in the answer is useful. The fact that some people are too lazy to read a few lines shouldn't justify removing useful information for the others.
    – Bastien
    Mar 11, 2016 at 10:03
  • 8
    Look, my hard drive is 1 TB and largely empty, and I have gobs of idle CPU cycles and RAM, and I want to save as much bash history as reasonably possible -- so when I need to look up that stupid command I haven't run in two years, I know it's in my bash history. To make it more concrete, let's say up to 50 MB of bash history. What values do you recommend?
    – CivFan
    Oct 21, 2016 at 20:56
  • 3
    @Matthew sorry, don't know it!
    – arturomp
    Nov 21, 2017 at 22:47
  • 2
    HISTFILESIZE=10 and HISTSIZE=10 – choosing the same value for two different variables does not really help when you try to show the difference between them. Considering that there are 10, 5 and 5, 10 cases shown later in the answer this one – 10, 10 – should be deleted. Dec 11, 2017 at 16:08
8

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
8
  • 6
    What values would you recommend if I want all history from all concurrent terminals to be saved to the bash history, forever? In other words, save everything, always, and never delete anything.
    – CivFan
    Nov 18, 2016 at 17:06
  • 10
    @CivFan: Set HISTSIZE=-1 and HISTFILESIZE=-1. Apr 6, 2017 at 13:19
  • 2
    This is dead wrong. Setting it to -1 will clear everything.
    – SineSwiper
    Feb 27, 2018 at 20:46
  • 4
    @BrendanByrd The bash man page says : Non-numeric values and numeric values less than zero inhibit truncation but then I didn't have the courage to check if this is true because I don't want to loose my history :-)
    – SebMa
    Feb 6, 2019 at 11:22
  • 7
    @SebMa I changed my settings to HISTSIZE='INFINITY' and HISTFILESIZE='ANDBEYOND'. Since they are non-numeric they save everything. Thanks for the tip!
    – Connor
    Feb 25, 2019 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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