6

I have been going through some videos on bash and history-related bash variables. There's a stackoverflow question that linked this video that mentions editing .bash_history.

I changed my $HISTFILE to a local file in shell A and changed cd to ls in the first line while still in shell A then I ran history and saw no change of cd.

    1  Sun 22/Dec/19 07:59:02 cd
    2  Sun 22/Dec/19 07:59:12 view ~/.bash_profile
    3  Sun 22/Dec/19 08:00:22 history 5
    4  Sun 22/Dec/19 08:00:58 vim ~/.bash_profile
    5  Sun 22/Dec/19 08:01:43 history 5
    6  Sun 22/Dec/19 08:01:59 history 4
    7  Sun 22/Dec/19 08:30:36 echo $HISTFILE
    8  Sun 22/Dec/19 08:30:48 vim $HISTFILE
    9  Sun 22/Dec/19 08:31:02 history

I then opened shell B ran history. Here's the output from the command. The new shells saws my change on the first line as ls.

    1  Sun 22/Dec/19 07:59:02 ls
    2  Sun 22/Dec/19 07:59:12 view ~/.bash_profile
    3  Sun 22/Dec/19 08:00:22 history 5
    4  Sun 22/Dec/19 08:00:58 vim ~/.bash_profile
    5  Sun 22/Dec/19 08:01:43 history 5
    6  Sun 22/Dec/19 08:01:59 history 4
    7  Sun 22/Dec/19 08:31:19 history

So does the bash process load up the history file into memory every time it is opened? Hence, if one has a very large $HISTFILE, wouldn't this be a big memory issue?

1 Answer 1

5

Yes, it does load it into memory (from man bash):

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.

And yes, theoretically that might be an issue, but these days most machines have more than enough memory to deal with it. For example, I have the following in my ~/.profile:

$ grep SIZE ~/.profile
export HISTSIZE=999999
export HISTFILESIZE=999999

That makes me keep 999999 in my history. So far, I am here:

$ history | wc
 109207  637303 4614715

Which gives me a ~/.bash_history file size of around 3.7M:

$ ls -l ~/.bash_history
-rw-r--r-- 1 terdon terdon 3846502 Dec 21 18:15 /home/terdon/.bash_history

That isn't even close to being noticeable on my laptop which has 32G of RAM.

In any case, that's just me. I have chosen to have a large history, so I may be sacrificing a tiny bit of speed for the advantages that gives me. If you don't want this, you can keep the default values instead (from man bash):

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

So, the default size is just 500 commands which really is negligible on today's machines. If even that is too much, you can always set it lower.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.