bash 是否在启动时将“HISTFILE”加载到内存中?

her*_*ity 5 bash command-history

我一直在浏览一些关于 bash 和history相关 bash 变量的视频。有一个 stackoverflow 问题链接了该视频,其中提到了编辑.bash_history.

$HISTFILE在外壳 A 中将my 更改为本地文件,并在仍在外壳 A 中时更改cdls第一行,然后我运行history并没有看到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
Run Code Online (Sandbox Code Playgroud)

然后我打开shell B 跑了history。这是命令的输出。新 shell 将我在第一行的更改视为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
Run Code Online (Sandbox Code Playgroud)

那么bash进程是否在每次打开历史文件时将其加载到内存中?因此,如果一个非常大的$HISTFILE,这不是一个很大的内存问题吗?

ter*_*don 5

是的,它确实将其加载到内存中(来自man bash):

\n\n
\n

启动时,历史记录从变量 HISTFILE 命名的文件(默认 ~/.bash_history)初始化。如有必要,由 HISTFILE 值指定的文件将被截断,以使其包含的行数不超过 HISTFILESIZE 值指定的行数。

\n
\n\n

是的,理论上这可能是一个问题,但现在大多数机器都有足够的内存来处理它。例如,我的 中有以下内容~/.profile

\n\n
$ grep SIZE ~/.profile\nexport HISTSIZE=999999\nexport HISTFILESIZE=999999\n
Run Code Online (Sandbox Code Playgroud)\n\n

这让我在我的历史记录中保留了 999999。到目前为止,我在这里:

\n\n
$ history | wc\n 109207  637303 4614715\n
Run Code Online (Sandbox Code Playgroud)\n\n

这给了我~/.bash_history大约 3.7M 的文件大小:

\n\n
$ ls -l ~/.bash_history\n-rw-r--r-- 1 terdon terdon 3846502 Dec 21 18:15 /home/terdon/.bash_history\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的 32G RAM 笔记本电脑上,这一点甚至还算不上明显。

\n\n

无论如何,那只是我。我选择拥有丰富的历史记录,因此我可能会牺牲一点点速度来换取我所获得的优势。如果您不希望这样,您可以保留默认值(来自man bash):

\n\n
   HISTFILESIZE\n          The maximum number of lines contained in the history file.  When\n          this  variable  is  assigned  a value, the history file is trun\xe2\x80\x90\n          cated, if necessary, to contain no  more  than  that  number  of\n          lines  by removing the oldest entries.  The history file is also\n          truncated to this size after writing it when a shell exits.   If\n          the  value  is  0,  the  history file is truncated to zero size.\n          Non-numeric values and numeric values  less  than  zero  inhibit\n          truncation.   The  shell  sets the default value to the value of\n          HISTSIZE after reading any startup files.\n   HISTSIZE\n          The  number  of commands to remember in the command history (see\n          HISTORY below).  If the value is 0, commands are  not  saved  in\n          the history list.  Numeric values less than zero result in every\n          command being saved on the history list  (there  is  no  limit).\n          The  shell  sets  the  default  value  to  500 after reading any\n          startup files.\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,默认大小仅为 500 个命令,这在当今的计算机上确实可以忽略不计。如果这太多了,您可以随时将其设置得更低。

\n