问题描述
同样的命令一次又一次地在我的历史记录中列出。我怎样才能避免这种情况?我知道可以通过HISTCONTROL
,但我没有找到正确的方法。
最佳解决方案
从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. 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.
因此,将以下行放在~/.bashrc
中:
export HISTCONTROL=ignoreboth:erasedups
次佳解决方案
坚持这个在你的~/.bashrc
:
export HISTCONTROL=ignoredups
你可以改用ignoreboth
。这是ignorespaces
(以空格开头的命令)和ignoredups
(重复)的缩写。
我更喜欢ignoredups
,因为我发现忽略前面有空格的命令的默认行为很烦人,当我从网站上复制一个命令并且它不会被保存,因为我不小心复制了一个空间……但是给每个人自己。
第三种解决方案
把这个放在~/.bashrc
中会在不同的会话中应用@ alvin的解决方案,就像wlell
HISTCONTROL=ignoredups:erasedups
shopt -s histappend
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
第四种方案
export HISTCONTROL=erasedups
第五种方案
将以下内容添加到您的~/.bashrc
:
export HISTCONTROL=ignoredups
要做到这一点,你可以使用这个命令:
nano ~/.bashrc