我想知道如何/是否可以重用我的终端历史记录中的命令,但在修改后的版本中。这是一个例子:
$ filter_script file2 > output_file2
$ # ...
# now run the same command, but replace '2' with '4'
$ filter_script file4 > output_file4
这是一个非常简单的示例,当然我可以简单地从历史记录中访问命令并手动替换两个2
,但是有没有更优雅的方法?
非常感谢您的宝贵时间!
最佳答案
如果只有 一个 您想替换的实例,bash(1)
有一个简单的功能,该功能首先在 csh(1)中引入
:
^old^new
将 old
的第一个实例替换为 new
:
$ filter_script file2 > output_file2
$ ^2^4
filter_script file4 > output_file2
如果你想替换所有实例,那需要更多的输入:
$ filter_script file2 > output_file2
$ !:gs/2/4/
filter_script file4 > output_file4
g
指定命令行上的全局 替换。 !
指的是历史记录中的一行——如果您想从更早的历史记录中提取一条命令,那么它可能会更具体。请参阅 bash(1)
关于事件指示符
的部分。
关于linux - 从终端历史记录中搜索和替换命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8224441/