linux – .bash_history:它总是记录我发出的每个命令吗?

前端之家收集整理的这篇文章主要介绍了linux – .bash_history:它总是记录我发出的每个命令吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我希望能够查看我的命令历史记录(一直回到用户的开头).

是否有保证.bash_history将继续附加?

如果存在文件开始被截断的限制(希望从头开始)有没有办法删除该限制?

最佳答案
有许多环境变量可以控制历史在bash中的工作方式. bash手册页的相关摘录如下:

   HISTCONTROL
          A colon-separated list of values controlling how commands are saved on the history list.  If the list of values includes ignorespace,lines  which
          begin  with  a space character are not saved in the history list.  A value of ignoredups causes lines matching the prevIoUs history entry to not be
          saved.  A value of ignoreboth is shorthand for ignorespace and ignoredups.  A value of erasedups causes all prevIoUs  lines  matching  the  current
          line  to be removed from the history list before that line is saved.  Any value not in the above list is ignored.  If HISTCONTROL is unset,or does
          not include a valid value,all lines read by the shell parser are saved on the history list,subject to the value of HISTIGNORE.   The  second  and
          subsequent lines of a multi-line compound command are not tested,and are added to the history regardless of the value of HISTCONTROL.
   HISTFILE
          The  name  of the file in which command history is saved (see HISTORY below).  The default value is ~/.bash_history.  If unset,the command history
          is not saved when an interactive shell exits.
   HISTFILESIZE
          The maximum number of lines contained in the history file.  When this variable is assigned a value,the history file is truncated,if necessary,by
          removing  the  oldest entries,to contain no more than that number of lines.  The default value is 500.  The history file is also truncated to this
          size after writing it when an interactive shell exits.
   HISTIGNORE
          A colon-separated list of patterns used to decide which command lines should be saved on the history list.  Each pattern is anchored at the  begin-
          ning  of  the line and must match the complete line (no implicit `*' is appended).  Each pattern is tested against the line after the checks speci-
          fied by HISTCONTROL are applied.  In addition to the normal shell pattern matching characters,`&' matches the prevIoUs history line.  `&'  may  be
          escaped  using  a  backslash; the backslash is removed before attempting a match.  The second and subsequent lines of a multi-line compound command
          are not tested,and are added to the history regardless of the value of HISTIGNORE.
   HISTSIZE
          The number of commands to remember in the command history (see HISTORY below).  The default value is 500.

直接回答您的问题:

没有保证,因为历史可以被禁用,一些命令可能不被存储(例如,以空格开始)并且可能对历史大小施加限制.

至于历史大小限制:如果你取消设置HISTSIZE和HISTFILESIZE:

unset HISTSIZE
unset HISTFILESIZE

你将阻止shell截断你的历史文件.但是,如果您运行的shell实例设置了这两个变量,它将在退出时截断您的历史记录,因此解决方案非常脆弱.如果您绝对必须维护长期shell历史记录,则不应依赖shell并定期将文件(例如使用cron作业)复制到安全位置.

历史截断始终首先删除最旧的条目,如上面的联机帮助页摘录中所述.

原文链接:https://www.f2er.com/linux/440502.html

猜你在找的Linux相关文章