2

In my terminal (bash 3), I sometimes use the quick substitution

^aa^bb^

^string1^string2^

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

For example

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

Which is really handy in some cases.

However, I found out that omitting the last ^ also works, i.e.

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

My question is: what is the consequence of omitting the closing ^? Can I safely leave it out or are there any caveats?

3

It can be omitted if it is the last character of the event line.

First, we check what ^string1^string2^ meaning from man bash:

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

So it's equivalent to s/string1/string2/. Read documentation of s modifier:

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.
  • Ah, that makes sense. Was typing long command, but your edit makes it obsolete, so I can do something like ^aa^bb^ cc, that is interesting. – Bernhard Jul 28 '14 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.