如何防止命令显示在Bash历史记录中? [英] How do I prevent commands from showing up in Bash history?

查看:120
本文介绍了如何防止命令显示在Bash历史记录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,当我运行诸如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的值是ignorespaceignoredups的简写.值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.

另请参阅:

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆