2025-01-10 06:00:04

图形化对?

HISTSIZE和HISTFILESIZE的区别是什么?

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

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


当前回答

在arturomp所说的基础上,努力使它更清楚一点。

假设你有2000多年的历史。

~$ history
    1  sdf
    2  fghdfgjf
    3  fghfghdf
   ..  ..
 2027  78
 2028  57
 2029  yu45u

你可以用HISTSIZE裁剪你所显示的内容

~$ HISTSIZE=5
~$ history
 2026  546
 2027  78
 2028  56
 2029  yu45u
 2030  HISTSIZE=5

现在,无论您键入多少命令,只会记录最后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

我们可以清楚地看到,我们的第一个命令(“ABC”)并不在历史中,因为只有最后5个命令被记录了下来。

现在,总历史记录存储在一个文件(.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

其他回答

简短的回答:

HISTSIZE是当您的bash会话正在进行时,存储在历史记录列表中的内存中的行数或命令数。

HISTFILESIZE是(a)在会话启动时允许在历史文件中使用的行数或命令数,以及(b)在bash会话结束时存储在历史文件中供将来会话使用的行数或命令数。

注意文件:在磁盘上和列表:在内存中之间的区别。

长一点的回答:

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

操作1: 粗略=10,粗略=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 step 1 with a HISTFILE of HISTFILESIZE=10.

操作2: 光标=10,组=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 step 1 with a HISTFILE of HISTFILESIZE=10.

操作3: 光标=5,组=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.

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

从男人狂欢页面来看:

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.

在arturomp所说的基础上,努力使它更清楚一点。

假设你有2000多年的历史。

~$ history
    1  sdf
    2  fghdfgjf
    3  fghfghdf
   ..  ..
 2027  78
 2028  57
 2029  yu45u

你可以用HISTSIZE裁剪你所显示的内容

~$ HISTSIZE=5
~$ history
 2026  546
 2027  78
 2028  56
 2029  yu45u
 2030  HISTSIZE=5

现在,无论您键入多少命令,只会记录最后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

我们可以清楚地看到,我们的第一个命令(“ABC”)并不在历史中,因为只有最后5个命令被记录了下来。

现在,总历史记录存储在一个文件(.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