使用历史扩展的速记快速替换有问题吗?

使用历史扩展的速记快速替换有问题吗?

在我的终端(bash 3)中,我有时使用快速替换

^aa^bb^

^string1^string2^

         Quick substitution.  Repeat the last command, replacing string1 with string2.  Equivalent to `!!:s/string1/string2/` (see Modifiers below).

例如

$ echo aa aa
aa aa
$ ^aa^bb^
echo bb aa
bb aa

在某些情况下这确实很方便。

但是,我发现省略最后一个^也可以,即

$ echo aa aa
aa aa
$ ^aa^bb
echo bb aa
bb aa

我的问题是:省略结束语会产生什么后果^?我可以安全地把它排除在外吗?或者有什么注意事项吗?

答案1

如果它是事件行的最后一个字符,则可以省略。

首先,我们检查以下^string1^string2^含义man bash

^string1^string2^
              Quick  substitution.   Repeat   the   previous   command,   
              replacing   string1   with   string2.    Equivalent   to
              ``!!:s/string1/string2/'' (see Modifiers below).

所以它相当于s/string1/string2/.阅读s修饰符的文档:

s/old/new/
          Substitute new for the first occurrence of old in the event line.  
          Any delimiter can be used in  place  of  /. The final  delimiter  
          is optional if it is the last character of the event line. The 
          delimiter may be quoted in old and new with a single backslash.  
          If & appears in new, it is replaced by old.  A single backslash 
          will quote the &. If old  is  null,  it is set to the last old 
          substituted, or, if no previous history substitutions took place, 
          the last string in a !?string[?]  search.

相关内容