如何防止命令显示在Bash历史记录中? [英] How do I prevent commands from showing up in Bash history?
问题描述
有时候,当我运行诸如rm -rf XYZ
之类的命令时,我不希望将其记录在Bash历史记录中,因为我可能不小心又由reverse-i-search
再次运行了同一命令.有防止这种情况发生的好方法吗?
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?
推荐答案
如果将HISTCONTROL
环境变量设置为ignoreboth
(通常默认设置),则带前导空格字符的命令将不会存储在历史记录中(以及重复记录).
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
$ echo test1
$ echo test2
$ history | tail -n2
1015 echo test1
1016 history | tail -n2
这是man bash
所说的:
HISTCONTROL
HISTCONTROL
用冒号分隔的值列表,用于控制如何在历史记录列表中保存命令.如果值列表包含ignorespace
,则以空格字符开头的行不会保存在历史记录列表中.值ignoredups
导致不保存与以前的历史记录条目匹配的行. ignoreboth
的值是ignorespace
和ignoredups
的简写.值erasedups
会导致在保存该行之前,将所有与当前行匹配的先前行从历史记录列表中删除.不在上面列表中的任何值都将被忽略.如果未设置HISTCONTROL
或不包含有效值,则由外壳解析器读取的所有行都将保存在历史记录列表中,并受HISTIGNORE
值的限制.多行复合命令的第二行和后续行未经测试,并且无论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
.
另请参阅:
- 为什么bash不存储那些以空格开头?在unix SE
- 为什么bash具有HISTCONTROL = ignorespace选项?在Unix SE上
- 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历史记录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!