Win*_*nix 11 command-line bash bash-history
前几天晚上,我正在阅读 AU Q&A 并使用了 bash 命令:
inxi -????
Run Code Online (Sandbox Code Playgroud)
问题是今天我不记得包含 ???? 的字符。我想将命令和参数放入我的文档电子表格中。基于这个答案(如何在终端中回忆历史)我使用了这个命令:
$ cat .bash_history | grep inxi
inxi -b
sudo apt install inxi
inxi -b
Run Code Online (Sandbox Code Playgroud)
然而,即使历史可以追溯到很久以前,我想要的命令也不在那里。inxi自从那段旧历史以来,我在终端中多次使用这些命令,但没有一个出现。
我也试过Ctrl+ R+inxi没有任何的运气。因为我一直打开多个终端窗口,历史是否与特定窗口相关联?
有没有不同的方式来grepbash 历史文件?
请注意,我没有在终端命令Space Bar前面加上前缀,以免它们从历史记录中消失。
ter*_*don 15
如果无法访问您的机器,我无法知道发生了什么,但这里有一个关于历史系统如何工作的简短说明,可能会帮助您弄清楚发生了什么。
每个打开的终端都有自己的历史缓冲区。$HISTFILE当终端关闭时,这些缓冲区会附加到您的(也可能是在缓冲区被填满时,但我不知道这种情况发生的频率)。现在,在历史记录中搜索命令的方法是简单地运行:
history | grep command
Run Code Online (Sandbox Code Playgroud)
但是,如果该命令在不同的 shell 中运行,您将不会在当前 shell 的历史记录中看到它。为了解决这个问题,你关闭所有打开的 shell,打开一个新的终端窗口并再次搜索你的历史记录。
如果这仍然没有帮助,您可能已经超过了存储在$HISTFILE. 的行为$HISTFILE由各种环境变量控制(请参阅man bash完整列表),但这里的相关变量是:
HISTSIZE
The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than
zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.
HISTFILESIZE
The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than
that number of lines by removing the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history
file is truncated to zero size. Non-numeric values and numeric values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after
reading any startup files.
Run Code Online (Sandbox Code Playgroud)
您将这些设置为越高的值,您将在$HISTFILE. 例如,我使用:
HISTSIZE=999999
HISTFILESIZE=999999
Run Code Online (Sandbox Code Playgroud)
如果要将历史从一个 shell 导入到另一个 shell,可以使用以下history命令:
$ help history | grep -E -- '-a|-r'
-a append history lines from this session to the history file
-r read the history file and append the contents to the history
Run Code Online (Sandbox Code Playgroud)
因此,运行history -a以从一个终端写入历史记录,然后从另一个终端history -w读取它。现在,运行history将显示两个 shell 的历史。
最后,您可以通过将这些行添加到您的~/.bashrc:
## history -a causes the last command to be written to the
## history file automatically and history -r imports the history
export PROMPT_COMMAND='history -a;history -r'
Run Code Online (Sandbox Code Playgroud)
我还建议您添加以下内容:
## Make Bash append rather than overwrite the history on disk:
shopt -s histappend
Run Code Online (Sandbox Code Playgroud)