“history -w”和关闭shell会话的动作有什么区别?

Sin*_*osh 7 command-line bash history

我做了一些关于历史的测试:

  1. 我跑了history-c然后注销了。这清除了当前 shell 的历史记录。

  2. 我跑了history -c && history -w。这删除了所有内容。

  3. 我通过 vi 编辑器删除了历史文件的全部内容:$vi ~/.bash_history. 然后我退出了。在下次登录时,当我history只运行最后一个 shell 会话的行或命令时。

这表明history -w当我们关闭 shell 会话时,和之间存在差异。

当我们关闭一个 shell 会话时到底发生了什么?

我认为将history -w内存内容覆盖到历史文件,并history -c删除内存内容。这样对吗?

Byt*_*der 11

在处理 Bash 历史时,我们有两种:

  1. 内存中的历史列表
  2. .bash_history磁盘上的文件

当 Bash 启动时(假设是默认配置),它会将您的.bash_history文件内容加载到内存中的历史列表中(如有必要,将其截断到配置的大小之后)。

然后您键入您的命令,这些命令仅附加到内存中的历史列表中。磁盘上的历史文件不会被触及。

默认情况下,定期退出 Bash 会话(不会强行杀死它或导致它以某种方式崩溃)会截断内存中的历史列表以适应配置的最大大小,然后仅附加当前 Bash 会话中的新条目(因为默认情况下该histappend选项已启用) 到磁盘上的历史文件,而不会删除已删除的条目或重新添加以前会话中的内容。


当您运行时,history -c您会清除内存中的完整历史列表,但这同样不会影响磁盘上的历史文件。

运行history -w会将内存中的当前历史列表写入磁盘上的历史文件。它不会追加新条目而是覆盖整个文件。因此,运行也history -c && history -w有效地清除了历史文件。


通过在正在运行的 Bash 会话中编辑来手动清除历史文件并没有达到预期的效果或永久删除整个历史,因为内存中的历史列表仍然包含历史文件中的所有旧条目将保持完整。

退出 Bash 会话时,历史文件将被历史列表中的数据重写。但是,由于默认情况下histappend启用该选项,只有来自当前 Bash 会话的新条目才会写入文件,旧的历史数据将被丢弃。您必须运行history -w才能将完整的历史列表保存到磁盘。


当 Bash shell 启动和退出时到底发生了什么可以读入man bash

HISTORY
       When the -o history option to the set builtin  is  enabled,  the  shell
       provides access to the command history, the list of commands previously
       typed.  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 com?
       mands (default 500) is saved.  The shell stores  each  command  in  the
       history  list  prior to parameter and variable expansion (see EXPANSION
       above) but after history expansion is performed, subject to the  values
       of the shell variables HISTIGNORE and HISTCONTROL.

       On startup, the history is initialized from the file named by the vari?
       able 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.   If  HISTFILE?
       SIZE  is unset, or set to null, a non-numeric value, or a numeric value
       less than zero, the history file is not truncated.   When  the  history
       file  is  read, lines beginning with the history comment character fol?
       lowed immediately by a digit are interpreted as timestamps for the pre?
       ceding history line.  These timestamps are optionally displayed depend?
       ing on the value of the HISTTIMEFORMAT variable.   When  a  shell  with
       history  enabled  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.  If the  HISTTIMEFORMAT  variable
       is  set,  time  stamps are written to the history file, marked with the
       history comment character, so they may be preserved across  shell  ses?
       sions.   This  uses  the history comment character to distinguish time?
       stamps from other history lines.  After saving the history, the history
       file is truncated to contain no more than HISTFILESIZE lines.  If HIST?
       FILESIZE is unset, or set to null, a non-numeric value,  or  a  numeric
       value less than zero, the history file is not truncated.
Run Code Online (Sandbox Code Playgroud)