如何防止命令出现在 Bash 历史记录中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6475524/
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
How do I prevent commands from showing up in Bash history?
提问by Jeeyoung Kim
Sometimes, when I run commands like rm -rf XYZ
, I don't want this to be recorded in Bash history, because I might accidentally run the same command again by reverse-i-search
. Is there a good way to prevent this from happening?
有时候,当我运行命令一样rm -rf XYZ
,我不希望这被记录在历史上猛砸,因为我可能会意外地再次运行相同的命令reverse-i-search
。有什么好的方法可以防止这种情况发生吗?
回答by kenorb
If you've set the HISTCONTROL
environment variable to ignoreboth
(which is usually set by default), commands with a leading space character will not be stored in the history (as well as duplicates).
如果您已将HISTCONTROL
环境变量设置为ignoreboth
(通常默认设置),带有前导空格字符的命令将不会存储在历史记录中(以及重复项)。
For example:
例如:
$ HISTCONTROL=ignoreboth
$ echo test1
$ echo test2
$ history | tail -n2
1015 echo test1
1016 history | tail -n2
Here is what man bash
says:
这是什么man 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 ofignoredups
causes lines matching the previous history entry to not be saved. A value ofignoreboth
is shorthand forignorespace
andignoredups
. A value oferasedups
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. IfHISTCONTROL
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 ofHISTIGNORE
. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value ofHISTCONTROL
.
组织控制
以冒号分隔的值列表,用于控制命令在历史列表中的保存方式。如果值列表包括
ignorespace
,以空格字符开头的行不会保存在历史列表中。值ignoredups
导致匹配先前历史条目的行不被保存。的值ignoreboth
是ignorespace
and 的简写ignoredups
。值erasedups
导致与当前行匹配的所有先前行在保存该行之前从历史列表中删除。任何不在上述列表中的值都将被忽略。如果HISTCONTROL
未设置,或不包含有效值,则 shell 解析器读取的所有行都保存在历史列表中,取决于值HISTIGNORE
. 不测试多行复合命令的第二行和后续行,无论 的值如何,它们都会添加到历史记录中HISTCONTROL
。
See also:
也可以看看:
- Why is bash not storing commands that start with spaces?at unix SE
- Why does bash have a HISTCONTROL=ignorespace option?at unix SE
- 为什么 bash 不存储以空格开头的命令?在 unix SE
- 为什么 bash 有 HISTCONTROL=ignorespace 选项?在 unix SE
回答by rhdoenges
In your .bashrc/.bash_profile/wherever you want, put export HISTIGNORE=' *'
. Then just begin any command you want to ignore with one space.
在你的 .bashrc/.bash_profile/ 任何你想要的地方,把export HISTIGNORE=' *'
. 然后只需用一个空格开始您要忽略的任何命令。
$ ls # goes in history
$ ls # does not
回答by bmatheny
Even better use HISTIGNORE
. This allows you to specify a set of patterns to be ignored (such as rm
). It is better (I think) than just piping all history to /dev/null
.
更好用HISTIGNORE
。这允许您指定一组要忽略的模式(例如rm
)。(我认为)这比将所有历史记录到/dev/null
.
回答by THESorcerer
kill -9 $$
I know that is not as best as the previous answers, but this will kill the current Bash shell without saving anything, useful when HISTCONTROL is not set by default, you forgot to set it, or pure and simple you forgot to put a leading space and you just typed in some passwords and don't want them to remain permanently in history.
我知道这不如之前的答案好,但这会杀死当前的 Bash shell 而不保存任何内容,当 HISTCONTROL 默认未设置时很有用,您忘记设置它,或者纯粹而简单,您忘记放置前导空格并且您只是输入了一些密码,但不希望它们永久保留在历史记录中。
This is the quick way, and something like erasing the history file is not as good because you need to do it outside a history saving shell (log in as different user and use su/sudo, creating a background job, etc.)
这是一种快速的方法,像擦除历史文件这样的事情并不好,因为您需要在历史保存 shell 之外执行此操作(以不同的用户身份登录并使用 su/sudo,创建后台作业等)
回答by Corey Henderson
You can do one of two things:
您可以执行以下两项操作之一:
export HISTFILE=/dev/null
Or, begin the command with a space.
或者,以空格开始命令。
回答by aledra
Or
或者
unset HISTFILE
(similar to the previous answer only shorter: export HISTFILE=/dev/null)
(类似于之前的答案只是更短:export HISTFILE=/dev/null)
回答by Jakub M.
At shell startup, I explicitly cleanup the history from the entries that I don't want to be there. For example, I don't want any rm -rf
in the history (it's trauma after removing a directory full of results processed overnight, just with a single Arrow-Up
+ Enter
:)
在 shell 启动时,我明确地从我不想在那里的条目中清除历史记录。例如,我不想要任何rm -rf
历史记录(删除一个包含一夜之间处理的结果的目录后的创伤,只需一个Arrow-Up
+ Enter
:)
I put the following snippet in my init file (works with .zshrc
, should also work with .bashrc
)
我将以下代码段放在我的 init 文件中(适用于.zshrc
,也应该适用于.bashrc
)
# ...
HISTFILE=~/.zshhistory
# ...
# remove dangerous entries from the shell history
temp_histfile="/tmp/$$.temp_histfile"
grep -v -P '^rm .*-rf' $HISTFILE > $temp_histfile
mv $temp_histfile $HISTFILE