bash HISTSIZE 与 HISTFILESIZE?

标签 bash unix

HISTSIZEHISTFILESIZE 有什么区别?

它们用于将 bash 历史记录扩展到默认的 500 行之外。

这里和其他论坛似乎都不清楚为什么需要它们。 ( Example 1 , Example 2 , Example 3 )。

最佳答案

简答:

HISTSIZE 是在您的 bash session 进行时存储在历史列表内存中的行数或命令数。

HISTFILESIZE 是 (a) 在 session 启动时历史文件中允许的行数或命令数,以及 (b) 在 session 结束时存储在历史文件中的行数或命令数bash session 用于 future 的 session 。

请注意 file: on disk - 和 list: in memory 之间的区别。

长答案:

以上所有信息+一些例子:

示例 1: HISTFILESIZE=10HISTSIZE=10

  1. 开始你的类(class)。
  2. 您的 HISTFILE(存储您的 bash 命令历史记录的文件)被截断为包含 HISTFILESIZE=10 行。
  3. 你写了 50 行。
  4. 在您的 50 个命令结束时,只有命令 41 到 50 在您的历史列表中,其大小由 HISTSIZE=10 决定。
  5. 您结束 session 。
  6. 假设 histappend 未启用,命令 41 到 50 将保存到您的 HISTFILE,该文件现在包含它在开头保留的 10 个命令以及 10 个新写入的命令。
  7. 您的 HISTFILE 被截断为包含 HISTFILESIZE=10 行。
  8. 您的历史记录中现在有 10 个命令 - 您在刚刚结束的 session 中刚刚输入的最后 10 个命令。
  9. 当您开始新 session 时,您会使用 HISTFILESIZE=10 的 HISTFILE 从第 1 步重新开始。

示例 2: HISTFILESIZE=10HISTSIZE=5

  1. 开始你的类(class)。
  2. 您的 HISTFILE(存储您的 bash 命令历史记录的文件)被截断为最多包含 HISTFILESIZE=10 行。
  3. 你写了 50 行。
  4. 在您的 50 个命令结束时,只有第 46 到 50 个命令在您的历史列表中,其大小由 HISTSIZE=5 决定。
  5. 您结束 session 。
  6. 假设 histappend 未启用,命令 46 到 50 将保存到您的 HISTFILE,该文件现在包含它在开头保留的 10 个命令以及 5 个新写入的命令。
  7. 您的 HISTFILE 被截断为包含 HISTFILESIZE=10 行。
  8. 现在您的历史记录中有 10 个命令 - 5 个来自之前的 session ,最后 5 个是您在刚刚结束的 session 中输入的。
  9. 当您开始新 session 时,您会使用 HISTFILESIZE=10 的 HISTFILE 从第 1 步重新开始。

示例 3: HISTFILESIZE=5HISTSIZE=10

  1. 开始你的类(class)。
  2. 您的 HISTFILE(存储您的 bash 命令历史记录的文件)被截断为最多包含 HISTFILESIZE=5 行。
  3. 你写了 50 行。
  4. 在您的 50 个命令结束时,只有命令 41 到 50 在您的历史列表中,其大小由 HISTSIZE=10 决定。
  5. 您结束 session 。
  6. 假设 histappend 未启用,命令 41 到 50 将保存到您的 HISTFILE 中,该文件现在包含它在开头保留的 5 个命令以及 10 个新写入的命令。
  7. 您的 HISTFILE 被截断为包含 HISTFILESIZE=5 行。
  8. 现在您的历史记录中有 5 个命令 - 您刚刚在刚刚结束的 session 中输入的最后 5 个命令。
  9. 当您开始一个新 session 时,您将使用 HISTFILESIZE=5 的 HISTFILE 从第 1 步重新开始。

信息来自 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.

man bash 页面:

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.

关于bash HISTSIZE 与 HISTFILESIZE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454837/

相关文章:

c++ - 更改 syslog 日志路径

linux - CUPS Web 界面的工作原理

linux - 在多个目录中创建多个文件

c++ - 控制台上的不同输出打印标准输出/重定向到文件

linux - 如何排除 tar 的绝对路径?

linux - 前台程序和后台程序有什么区别?

linux - Bash 脚本,通过从 PID 文件中提取来终止进程

打印当前目录中文件名的 C 实用程序 (Unix)

c - 如何从c中的目录中仅获取*.txt文件名并将其写入char **数组?

bash - 如何在 bash 脚本中正确捕获 SIGQUIT?