如何设置命令历史召回次数

Gil*_*les 19 bash command-history

我正在使用 bash。要浏览我的命令历史记录,我正在调用history我认为正在调用同名 Gnu 程序的命令。(我不知道是否有更好的 bash 特定方式)。

在我的 .bashrc 中,我目前有一行export PROMPT_COMMAND='history -a'来保存我正在运行的多个 bash 会话的历史记录。

如果我做历史记录,我目前只看到 524 个条目。这个可以配置吗?我想将其增加到更大的数字,比如 2000。

ter*_*don 22

首先,history bash特定的方式,没有更好的。该history命令是一个内置的 bash,您可以通过运行看到

$ type history 
history is a shell builtin
Run Code Online (Sandbox Code Playgroud)

现在,它记住的命令数量由HISTSIZE变量控制。要将其设置为更大的数字,请将这一行添加到您的.profile(为什么这是一个比 更好的地方.bashrc,请参见此处):

export HISTSIZE=2000
Run Code Online (Sandbox Code Playgroud)

从现在开始,history将返回您最近运行的 2000 个命令。


Bra*_*ley 5

HISTSIZE变量指示在运行历史记录中保存多少命令,并HISTFILESIZE确定 shell 退出后保存运行历史记录中的多少命令。


Evg*_*gin 5

是的,man bash说:

HISTSIZE - 命令历史记录中要记住的命令数

但是有一个Readline变量:history-size

设置历史列表中保存的最大历史条目数。如果设置为零,则删除任何现有历史条目并且不保存新条目。如果设置为小于零的值,则历史条目的数量不受限制。默认情况下,历史条目的数量没有限制。

您可以history-size使用HISTSIZE=1000,bind 'set history-size 1000'或在您的以下行中进行设置~/.inputrcset history-size 1000

例子

HISTSIZE=1000
bind 'set history-size 0'
echo $HISTSIZE # prints 1000
bind -v | grep history-size # prints set history-size 0
history # prints nothing

bind 'set history-size 0'
HISTSIZE=1000
echo $HISTSIZE # prints 1000
bind -v | grep history-size # prints set history-size 1000
history # prints    13  echo $HISTSIZE\n14  bind -v | grep history-size\n15  history
Run Code Online (Sandbox Code Playgroud)

history-size可用时间bash-4.0-alpha更改