bashは起動時に「HISTFILE」をメモリにロードしますか?

bashは起動時に「HISTFILE」をメモリにロードしますか?

history私はbashと関連するbash変数のビデオを見ました。 stackoverflow質問リンクがありますこの動画それは編集に言及しました.bash_history

$HISTFILEmyファイルをシェルAのローカルファイルに変更し、最初の行をシェルAのファイルcdに変更してから実行します。lshistorycd

    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

その後、シェルBを開きますhistory。これはコマンドの出力です。新しいシェルは最初の行を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

その後、bashプロセスが開くたびに履歴ファイルがメモリにロードされますか?したがって、非常に大きい場合、$HISTFILEメモリの問題は大きくありませんか?

ベストアンサー1

はい、(から)メモリにロードされますman bash

起動時の履歴は、HISTFILE変数(デフォルトは〜/ .bash_history)という名前のファイルで初期化されます。必要に応じて、HISTFILE値で指定されたファイルは、HISTFILESIZE値で指定された行数を超えないように切り捨てられます。

はい、理論的には問題になるかもしれませんが、今日のほとんどのコンピュータにはそれを処理するのに十分なメモリがあります。たとえば、私の中には次のものがあります~/.profile

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

これにより、私の記録には999999が残ります。これまで私はここにいる:

$ history | wc
 109207  637303 4614715

~/.bash_historyこれにより、約3.7Mのファイルサイズが得られます。

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

私の32G RAMノートブックでは、この現象は目立たない。

とにかくそれは私だ。私は豊かな歴史を選んだので、私が得る利点のために少しの速度を犠牲にすることができます。これを望まない場合は、デフォルト値(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.

したがって、デフォルトのサイズはわずか500のコマ​​ンドですが、これは今日のコンピュータでは実際に無視できるレベルです。これが多すぎると、いつでも低く設定できます。

おすすめ記事