AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 832335
Accepted
Sinoosh
Sinoosh
Asked: 2016-10-03 12:09:21 +0800 CST2016-10-03 12:09:21 +0800 CST 2016-10-03 12:09:21 +0800 CST

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

  • 772

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

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

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

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

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

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

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

command-line
  • 1 1 个回答
  • 6481 Views

1 个回答

  • Voted
  1. Best Answer
    Byte Commander
    2016-10-03T12:57:30+08:002016-10-03T12:57:30+08:00

    在处理 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.
    
    • 11

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve