如何停止重复命令的命令号递增?

如何停止重复命令的命令号递增?

问题

谢谢这个问题,如果我多次写入相同的命令,我成功禁止我的 bash 多次存储相同的命令。

但是,还有一个细节我无法解决:我的提示符包含\#打印每个命令的命令号的特殊字符。而且每次我输入命令时,它仍然会自动增加,无论是否重复,即使我的历史记录没有填满。

样本

在我的中~/.bashrc,我的提示如下所示:

export PS1='aracthor \# >'

当我多次写入相同的命令时,命令编号会增加:

aracthor 1 >pwd
/home/aracthor
aracthor 2 >pwd
/home/aracthor
aracthor 3 >

我怎样才能避免命令号增加?

我正在使用 Ubuntu 14.04.02,declare -p HISTCONTROL输出是:

declare -x HISTCONTROL="ignoredups:erasedups"

答案1

通常\!会按您的需要增加,但当您达到内存中保存的行数的历史记录限制 HISTSIZE 时,它将停止。您可以尝试更大的 HISTSIZE。

或者,您可以设置更大的 HISTSIZE 和 HISTFILESIZE,并在每次提示之前运行命令来保存历史记录:

PROMPT_COMMAND='history -w'

然后PS1您可以运行命令来计算文件中的历史记录行数:

PS1='\w #=\# !=\! file=$(wc -l <$HISTFILE) $ '

答案2

阅读man bash内置set命令以及相关部分HISTCONTROL

       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 regard‐
          less of the value of HISTCONTROL.

在我的~/.bashrc我有:

export HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups

以下是一个例子:

w3@aardvark:~(0)$ echo $HISTCONTROL
ignoreboth
w3@aardvark:~(0)$ date
Sat Jun 27 19:50:56 EDT 2015
w3@aardvark:~(0)$ date
Sat Jun 27 19:50:57 EDT 2015
w3@aardvark:~(0)$ date
Sat Jun 27 19:50:58 EDT 2015
w3@aardvark:~(0)$ history
    1  cd
    2  echo $HISTCONTROL
    3  date
    4  history
w3@aardvark:~(0)$  

date之后的执行history将被记录和统计。

相关内容