bash HISTSIZE 与 HISTFILESIZE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19454837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash HISTSIZE vs. HISTFILESIZE?
提问by arturomp
What is the difference in HISTSIZE
vs. HISTFILESIZE
?
HISTSIZE
vs. 有HISTFILESIZE
什么区别?
They are used to extend bash history beyond the default 500 lines.
它们用于将 bash 历史记录扩展到默认的 500 行之外。
There seems to be lack of clarity here and in other forums about why they are both needed. (Example 1, Example 2, Example 3).
回答by arturomp
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.
HISTSIZE
是当您的 bash 会话正在进行时,历史列表中存储在内存中的行数或命令数。
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.
HISTFILESIZE
是 (a) 会话启动时历史文件中允许的行数或命令数,以及 (b) 在 bash 会话结束时存储在历史文件中以供将来会话使用的行数或命令数。
Notice the distinction between file
: on disk - and list
: in memory.
注意file
磁盘上的 : 和list
内存中的 :之间的区别。
Long answer:
长答案:
All the info above + some examples:
以上所有信息+一些示例:
Example 1:
HISTFILESIZE=10
and HISTSIZE=10
示例 1:
HISTFILESIZE=10
和HISTSIZE=10
- You start your session.
- Your HISTFILE (file that stores your bash command history), is truncated to contain HISTFILESIZE=10 lines.
- You write 50 lines.
- At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
- You end your session.
- 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. - Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
- You now have 10 commands in your history - the last 10 that you just typed in the session you just finished.
- When you start a new session, you start over at 1 with a HISTFILE of HISTFILESIZE=10.
- 你开始你的会话。
- 您的 HISTFILE(存储 bash 命令历史记录的文件)被截断为包含 HISTFILESIZE=10 行。
- 你写了 50 行。
- 在你的 50 个命令的末尾,只有 41 到 50 个命令在你的历史列表中,其大小由 HISTSIZE=10 决定。
- 您结束会话。
- 假设
histappend
未启用,命令 41 到 50 将保存到您的 HISTFILE 中,该文件现在具有它在开头保存的 10 个命令以及 10 个新写入的命令。 - 您的 HISTFILE 被截断为包含 HISTFILESIZE=10 行。
- 现在,您的历史记录中有 10 个命令 - 您刚刚在刚刚完成的会话中输入的最后 10 个命令。
- 当您开始一个新会话时,您从 1 开始,HISTFILE 的 HISTFILESIZE=10。
Example 2:
HISTFILESIZE=10
and HISTSIZE=5
示例 2:
HISTFILESIZE=10
和HISTSIZE=5
- You start your session.
- Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=10 lines.
- You write 50 lines.
- At the end of your 50 commands, only commands 46 to 50 are in your history list, whose size is determined by HISTSIZE=5.
- You end your session.
- 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. - Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
- 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.
- When you start a new session, you start over at 1 with a HISTFILE of HISTFILESIZE=10.
- 你开始你的会话。
- 您的 HISTFILE(存储 bash 命令历史记录的文件)被截断为最多包含 HISTFILESIZE=10 行。
- 你写了 50 行。
- 在你的 50 个命令的末尾,只有 46 到 50 个命令在你的历史列表中,其大小由 HISTSIZE=5 决定。
- 您结束会话。
- 假设
histappend
未启用,命令 46 到 50 将保存到您的 HISTFILE 中,该文件现在包含开头的 10 个命令以及 5 个新写入的命令。 - 您的 HISTFILE 被截断为包含 HISTFILESIZE=10 行。
- 现在,您的历史记录中有 10 个命令 - 5 个来自前一个会话,最后 5 个是您刚刚在刚刚完成的会话中输入的。
- 当您开始一个新会话时,您从 1 开始,HISTFILE 的 HISTFILESIZE=10。
Example 3:
HISTFILESIZE=5
and HISTSIZE=10
示例 3:
HISTFILESIZE=5
和HISTSIZE=10
- You start your session.
- Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=5 lines.
- You write 50 lines.
- At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
- You end your session.
- 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. - Your HISTFILE is truncated to contain HISTFILESIZE=5 lines.
- You now have 5 commands in your history - the last 5 that you just typed in the session you just finished.
- When you start a new session, you start over at step 1 with a HISTFILE of HISTFILESIZE=5.
- 你开始你的会话。
- 您的 HISTFILE(存储 bash 命令历史记录的文件)被截断为最多包含 HISTFILESIZE=5 行。
- 你写了 50 行。
- 在你的 50 个命令的末尾,只有 41 到 50 个命令在你的历史列表中,其大小由 HISTSIZE=10 决定。
- 您结束会话。
- 假设
histappend
未启用,命令 41 到 50 将保存到您的 HISTFILE 中,该文件现在包含开头的 5 个命令以及 10 个新写入的命令。 - 您的 HISTFILE 被截断为包含 HISTFILESIZE=5 行。
- 现在,您的历史记录中有 5 个命令 - 您刚刚在刚刚完成的会话中键入的最后 5 个命令。
- 当您开始一个新的会话时,您会从第 1 步开始使用 HISTFILESIZE=5 的 HISTFILE。
Info from elixir_sinari:
来自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 upto a maximum of $HISTSIZE commands.
当您键入命令时,历史“文件”不会更新。这些命令单独存储在“列表”中(由 history 命令访问)。这些存储命令的数量由 HISTSIZE 值控制。当 shell(交互式)退出时,最后的 $HISTSIZE 行被复制/附加到该“列表”中的 $HISTFILE。如果设置了 HISTFILESIZE,则在此操作之后,确保 $HISTFILE 中仅存在 $HISTFILESIZE 行(最新)。当 shell 启动时,“列表”从 $HISTFILE 到最多 $HISTSIZE 命令初始化。
And from the man bash
page:
从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.
HISTSIZE 变量的值用作保存在历史列表中的命令数。保存最后一个 HISTSIZE 命令的文本(默认 500)。(……)
启动时,历史从由变量 HISTFILE(默认 ~/.bash_history)命名的文件初始化。如有必要,由 HISTFILE 值命名的文件将被截断,以包含不超过 HISTFILESIZE 值指定的行数。(...) 当交互式 shell 退出时,最后的 $HISTSIZE 行会从历史列表复制到 $HISTFILE。如果启用了 histappend shell 选项(请参阅下面 SHELL BUILTIN COMMANDS 下的 shopt 说明),这些行将附加到历史文件中,否则历史文件将被覆盖。如果未设置 HISTFILE,或者历史文件不可写,则不会保存历史。(...) 保存历史记录后,历史文件被截断以包含不超过 HISTFILESIZE 行。如果未设置 HISTFILESIZE,
回答by Pithikos
Building on top of what arturomp have said and in an effort to make it a bit clearer.
建立在 arturomp 所说的基础上,并努力使其更加清晰。
Assumming you have 2000-something long history..
假设你有 2000 年的悠久历史..
~$ history
1 sdf
2 fghdfgjf
3 fghfghdf
.. ..
2027 78
2028 57
2029 yu45u
You can cut down what you are shown with HISTSIZE
您可以减少显示的内容 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.
现在,无论您键入多少命令,都只会记录最后 5 个命令。
~$ 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.
我们可以清楚地看到我们的第一个命令(“ABC”)不在历史记录中,因为只记录了最后 5 个命令。
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:
现在,总历史记录存储在文件 ( .bash_history
) 中,您可以使用HISTFILESIZE
. 例如2033
HISTFILESIZE
,在我的情况下,我会有这个:
~$ history
1 fghfghdf
2 gegege
3 gege
.. ..
2028 HISTSIZE=5
2029 GGH
2030 GSDHFG
2031 JFDR
2032 ABSDDS
2033 AHFGHFD