.bash_history:它是否总是logging我发出的每一个命令?

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

是否有任何保证.bash_history将继续被附加到?

如果文件开始被截断(希望从头开始)有一个限制,是否有办法消除这个限制?

有许多环境变量可以控制历史在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. 

直接回答你的问题:

不是没有保证,因为可以禁用历史记录,有些命令可能不会被存储(例如以空格开始),并且历史大小可能会受到限制。

至于历史的大小限制:如果你没有设置HISTSIZEHISTFILESIZE

 unset HISTSIZE unset HISTFILESIZE 

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

历史截断总是首先删除最旧的条目,如上面的manpage摘录所述。

man bash是你的朋友: HISTFILESIZE变量:

历史文件中包含的最大行数。 当这个变量被分配一个值时,历史文件被截断,如果有必要的话,通过删除最旧的条目来包含不超过这个行数。 默认值为500.在交互式shell退出时,历史文件在写入后也会被截断为这个大小。

如果您希望所有会话中的所有命令都立即附加到历史记录中(以便您可以立即在另一个会话中使用一个会话中的命令),则可以将以下几行放入.bashrc中:

 unset HISTSIZE unset HISTFILESIZE HISTCONTROL=erasedups # append to the history file, don't overwrite it shopt -s histappend # multi-line commands should be stored as a single command shopt -s cmdhist # sharing of history between multiple terminals # histfile has to be read and saved after each command execution PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND" 

有关这些命令的更多信息可以在这里找到: 在多个终端窗口中保留bash历史记录

另请注意。 默认情况下,如果你使用多个登录会话(通常是2或3个putty窗口登录到同一个服务器),他们不会看到他人的历史。

另外,看起来当我退出所有的shell时,最后一个退出的是保存的文件(即当我第二天开始工作时可见)。 所以我认为bash是保持历史记忆,然后在退出时冲洗文件,至少这是我的猜测基于我所看到的。

Adam建议不设置大小限制并不能削减它,但是你可以通过在/ etc / profile中设置readonly标志来避免shell的其他实例改变HISTSIZE,HISTFILESIZE …。

 unset HISTSIZE unset HISTFILESIZE readonly HISTFILE HISTFILESIZE HISTSIZE HISTCONTROL HISTIGNORE HISTTIMEFORMAT