Quick Substitution
Problem
Here’s a simpler syntax for making substitutions in your previously-executed command and running the modified result.
Solution
Use the caret (^) substitution mechanism:
$ /usr/bin/somewhere/someprog -g -A -yknot -w /tmp/soforthandsoon ... $ ^-g -A^-gB^ /usr/bin/somewhere/someprog -gB -yknot -w /tmp/soforthandsoon
You can always just use the arrow keys to navigate your history and commands, but for long commands on slow links this syntax is great once you get used to it.
Discussion
Write the substitution on the command line by starting with a caret (^) and then the text you want replaced, then another caret and the new text. A trailing (third) caret is needed only if you want to add more text at the end of the line, as in:
$ /usr/bin/somewhere/someprog -g -A -yknot ... $ ^-g -A^-gB^ /tmp^ /usr/bin/somewhere/someprog -gB -yknot /tmp
If you want to remove something, substitute an empty value; i.e., don’t put anything for the new text. Here are two examples:
$ /usr/bin/somewhere/someprog -g -A -yknot /tmp ... $ ^-g -A^^ /usr/bin/somewhere/someprog -yknot /tmp ... $ ^knot^ /usr/bin/somewhere/someprog -gA -y /tmp ... $
The first example uses all three carets. The second example leaves off the third caret; since we want to replace the “knot” with nothing, we just end the line with a newline (the Enter key).
The use of the caret substitution is just plain handy. Many
bash users find it easier to use than !!:s/…/…/
syntax. What do you think?
See Also
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.