2

:& repeats bash history expansion substitution. For example:

$ echo 1 1 1 1 1
1 1 1 1 1
$ ^1^9
echo 9 1 1 1 1
9 1 1 1 1
$ echo 1 1 1 1 1
1 1 1 1 1
$ ^1^9^:&
echo 9 9 1 1 1
9 9 1 1 1
$ echo 1 1 1 1 1
1 1 1 1 1
$ ^1^9^:&:&:&:&
echo 9 9 9 9 9
9 9 9 9 9
$ 

As seen above, one needs to type :& for each repetition. Is there a way to repeat substitution for all the matches automatically? I mean something like:

$ echo 1 1 1 1 1
1 1 1 1 1
$ ^1^9^:*
echo 9 9 9 9 9
9 9 9 9 9
$ 
6

You can use event designators instead:

$ echo 1 1 1 1 1
1 1 1 1 1
$ !!:gs/1/9/
echo 9 9 9 9 9
9 9 9 9 9

!! is a word designator which expands to the previous command run. s and g are modifiers:

After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a ‘:’.

s/old/new/

Substitute new for the first occurrence of old in the event line. [...]

g/a

Cause changes to be applied over the entire event line. Used in conjunction with ‘s’, as in gs/old/new/, or with ‘&’.

So, !!:gs/1/9/ means "run the previous command, replacing all 1s with 9s".

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.