Linux .bash_history:它是否总是记录我发出的每个命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8168676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.bash_history: Does it always record every command I ever issue?
提问by Steven Lu
I'd like to be able to look through my command history (all the way back to the beginning of the user).
我希望能够查看我的命令历史记录(一直回到用户的开头)。
Is there any guarantee that .bash_history will continue to be appended to?
是否有任何保证 .bash_history 将继续附加到后面?
If there is a limit where the file will start to be truncated (hopefully from the beginning) is there a way to remove that limit?
如果文件将开始被截断(希望从一开始)有限制,有没有办法消除该限制?
采纳答案by Adam Zalcman
There is a number of environment variables that control how history works in bash. Relevant excerpt from bash manpage follows:
有许多环境变量可以控制历史在 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.
To answer your questions directly:
直接回答您的问题:
No there isn't a guarantee, since history can be disabled, some commands may not be stored (e.g. starting with a whitespace) and there may be a limit imposed on the history size.
不,没有保证,因为历史可以被禁用,一些命令可能不会被存储(例如以空格开头)并且可能对历史大小施加限制。
As for the history size limitation: if you unset HISTSIZE
and HISTFILESIZE
:
至于历史大小限制:如果您取消设置HISTSIZE
和HISTFILESIZE
:
unset HISTSIZE
unset HISTFILESIZE
you'll prevent the shell from truncating your history file. However, if you have an instance of a shell running that has these two variables set, it will truncate your history while exiting, so the solution is quite brittle. In case you absolutely must maintain long term shell history, you should not rely on shell and copy the files regularly (e.g. using a cron job) to a safe location.
您将防止 shell 截断您的历史文件。但是,如果您有一个运行的 shell 实例并设置了这两个变量,它会在退出时截断您的历史记录,因此该解决方案非常脆弱。如果您绝对必须保持长期的 shell 历史记录,您不应该依赖 shell 并定期(例如使用 cron 作业)将文件复制到安全位置。
History truncation always removes oldest entries first as stated in the manpage excerpt above.
历史截断总是首先删除最旧的条目,如上面的联机帮助页摘录中所述。
回答by thiton
man bash
is your friend: The HISTFILESIZE
variable:
man bash
是你的朋友: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.
历史文件中包含的最大行数。当这个变量被赋值时,如果有必要,通过删除最旧的条目来截断历史文件,以包含不超过该数量的行。默认值为 500。当交互式 shell 退出时,历史文件在写入后也会被截断到这个大小。
回答by Sodved
Also note. By default if you work with multiple login sessions (I usually 2 or 3 putty windows logged into the same server), they do NOT see eachothers history.
还要注意。默认情况下,如果您使用多个登录会话(我通常将 2 或 3 个腻子窗口登录到同一台服务器),他们不会看到彼此的历史记录。
Also it appears that when I exit all the shells its the last one to exit is the one which is saved back the file (i.e. is visible when I start work the next day). So I assume bash is keeping the history in memory and then flushes to the file on exit, at least thats my guess based on what I see.
此外,当我退出所有 shell 时,最后一个退出的 shell 是保存回文件的 shell(即,当我第二天开始工作时可见)。所以我假设 bash 将历史记录保存在内存中,然后在退出时刷新到文件中,至少这是我根据我看到的猜测。
回答by Aas
Adam suggested that unsetting size limits doesn't cut it, but you can avoid other instances of shell to change HISTSIZE, HISTFILESIZE, ... by setting readonly flag on them in /etc/profile.
Adam 建议取消设置大小限制并不会减少它,但是您可以通过在 /etc/profile 中设置 readonly 标志来避免其他 shell 实例更改 HISTSIZE、HISTFILESIZE 等。
unset HISTSIZE
unset HISTFILESIZE
readonly HISTFILE HISTFILESIZE HISTSIZE HISTCONTROL HISTIGNORE HISTTIMEFORMAT
回答by Ansa211
If you want all commands from all sessions appended to the history instantly (so that you can use commands from one session immediately in another), you can put the following lines into your .bashrc :
如果您希望将所有会话中的所有命令立即附加到历史记录中(以便您可以立即在另一个会话中使用来自一个会话的命令),您可以将以下几行放入您的 .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"
More information about these commands can be found here: Preserve bash history in multiple terminal windows
可以在此处找到有关这些命令的更多信息:在多个终端窗口中保留 bash 历史记录