Luc*_*ulz 6 command-line shell
如果我在终端中运行这样的命令
$ tyop --count 3 --exact haveibeenpwned
Run Code Online (Sandbox Code Playgroud)
该命令返回错误代码,例如
command not found: tyop
,
如何重新运行最后一个命令,将命令行参数保留--count 3 --exact haveibeenpwned
为另一个命令名称(例如typo
而不是tyop
)?
$ typo --count 3 --exact haveibeenpwned
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我正在寻找快捷方式或 shell 函数,例如!!
或!^
。
Qua*_*odo 12
typo !*
Run Code Online (Sandbox Code Playgroud)
从man bash
:
Word Designators
Word designators are used to select desired words from the event. A :
separates the event specification from the word designator. It may be
omitted if the word designator begins with a ^, $, *, -, or %. Words
are numbered from the beginning of the line, with the first word being
denoted by 0 (zero). Words are inserted into the current line
separated by single spaces.
* All of the words but the zeroth. This is a synonym for `1-$'.
It is not an error to use * if there is just one word in the
event; the empty string is returned in that case.
Run Code Online (Sandbox Code Playgroud)
除了按向上箭头和手动编辑输入行等显而易见的事情之外,GNU Readline(和其他几个程序使用的bash
)还内置了一些有用的历史编辑功能。 !*
其他答案中提到的就是其中之一。
另一个是使用 的字符串替换功能^
。从man bash
:
^string1^string2^
快速替换。重复上一个命令,将 string1 替换为
string2。相当于!!:s^string1^string2^
(参见下面的修饰符)。
^tyop^typo^
使用此功能,您可以通过键入并按 Enter 键来修复您的命令。
bash 的历史可以做的事情还有很多。运行man bash
并搜索HISTORY EXPANSION
并阅读该部分以及所有子部分(Event Designators
、Word Designators
和Modifiers
)。
顺便说一句,也值得阅读标题为 的部分READLINE
,或者至少浏览一下它以大致了解它的功能。有关 readline 的完整文档可以在https://tiswww.cwru.edu/php/chet/readline/rltop.html和https://tiswww.cwru.edu/php/chet/readline/readline.html找到