cht*_*ous 5 bash command-history
echo "This is a sentence. " !#:* !#:1->text3
Run Code Online (Sandbox Code Playgroud)
我无法理解上述代码行如何echo多次重复该命令。我知道它正在使用bash的历史功能,但我找不到关于!#:*或 的任何文档 !#:1。有人可以为我解释一下吗?
G-M*_*ca' 13
是的,这是在使用历史。
!#是一个历史事件指示符
,指的是到目前为止键入的整个命令行。
:*是一个词(范围)指示符,它指的是除第 0 个之外的所有词。所以,在你输入之后echo "This is a sentence. ",然后!#:*扩展到"This is a sentence. ". 并且(其中x和y是整数)是一个字(范围)指示符,它指的是字号x到字号y。如果y被省略 ( ),这将被解释为从第二个到最后一个单词的单词编号x。所以,在你的“到目前为止输入的整个命令行”之后
x-yx-
echo "This is a sentence. " "This is a sentence. "
Run Code Online (Sandbox Code Playgroud)
然后!#:1-扩展为"This is a sentence. ",因为每个带引号的"This is a sentence. "字符串都算作一个单词,因此!#:1-等效于!#:1(仅单词编号 1)。所以你最终得到
echo "This is a sentence. " "This is a sentence. " "This is a sentence. " >text3
Run Code Online (Sandbox Code Playgroud)
命令中的 the-和 the>一起出现只是一种混淆;他们不互动。以及“这是一个句子”这一事实。被引用掩盖了正在发生的事情;如果你说
echo This is a sentence. !#:* !#:1-
Run Code Online (Sandbox Code Playgroud)
它会扩展到
echo This is a sentence. This is a sentence. !#:1-
Run Code Online (Sandbox Code Playgroud)
然后到
echo This is a sentence. This is a sentence. This is a sentence. This is a
Run Code Online (Sandbox Code Playgroud)
(因为!#:1-从倒数第二个单词扩展到第 1 个单词。)