有时,当我运行命令像rm -rf XYZ,我不希望这被记录在bash历史记录,因为我可能不小心再次通过reverse-i-search运行相同的命令。有没有好的方法来防止这种情况发生?
如果您将HISTCONTROL设置为ignoreboth(通常是默认设置),则具有前导空格字符的命令不会存储在历史记录中(以及重复的)。
例如:
$ echo test1
$ echo test2
$ history | tail -n2
1015 echo test1
1016 history | tail -n2
这里是男人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
.
也可以看看:
> Why is bash not storing commands that start with spaces?在unix SE
> Why does bash have a HISTCONTROL=ignorespace option? at unix SE