Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace ALL instances of a string in the previous command in Bash? [duplicate]

Tags:

bash

If I have just entered the following command in Bash:

echo foo 

I can change foo to bar by typing:

^foo^bar 

Which results in the following command being executed:

echo bar 

Now if I enter:

echo foo foo 

Is there a way to change both instances of foo to bar just by using the caret (^) operator?

Additionally, are there man pages for shell operators like ^? man ^ results in "No manual entry for ^".

like image 356
mattjames Avatar asked Jan 27 '10 18:01

mattjames


People also ask

How do I replace a string with another string in Shell?

Replace String in a File with the `sed` Command'-i' option is used to modify the content of the original file with the replacement string if the search string exists in the file. 's' indicates the substitute command. 'search_string' contains the string value that will be searched in the file for replacement.

How do you repeat a previous command in Linux?

1) Ctrl + P This is the most dependable shortcut in Linux to execute the last run command in the terminal. Just press the Ctrl and P keys together to fill the prompt with the last executed command and you are ready to go.


1 Answers

That particular feature is called quick substitution; its documentation can be found in the Event Designators section of the Bash Manual. You can't do what you want with quick substitution; you'll have to resort to something slightly more verbose:

!!:gs/foo/bar/ 
like image 99
Adam Rosenfield Avatar answered Sep 29 '22 22:09

Adam Rosenfield