Bash历史记录重新运行:可能的命令,以避免使用!爆炸!?

dre*_*mac 4 bash shell scripting grep history

场景:

你正在做你的每日Bash shell的东西.您想要运行上一个命令,因此键入:

history | grep foocommand
Run Code Online (Sandbox Code Playgroud)

然后,您会得到一份列表,其中包含foocommand您为历史记录保留的所有内容所做的所有内容,如下所示:

  585 foocommand --baz --bleet
  750 foocommand | grep quux
  987 history grep | foocommand
Run Code Online (Sandbox Code Playgroud)

您决定要运行命令585,因此键入

  !585
Run Code Online (Sandbox Code Playgroud)

问题:只是出于好奇,有没有办法让最后一步走出这个等式并仍然得到预期的结果?如果有办法说出来会很好:

 "grep through history and automatically run the first item on the list"
Run Code Online (Sandbox Code Playgroud)

要么

"grep through history and let me choose which item to run using the arrow keys"
Run Code Online (Sandbox Code Playgroud)

Tom*_*icz 5

^r(即CTLR-r),键入foocommandEnter;)这将搜索历史记录并显示最新的匹配条目