Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

^word^replacement^ on all matches in Bash?

Tags:

bash

shell

sh

To clarify, I am looking for a way to perform a global search and replace on the previous command used. ^word^replacement^ only seems to replace the first match.

Is there some set option that is eluding me?

like image 457
pisswillis Avatar asked Mar 16 '09 02:03

pisswillis


People also ask

How do I replace words in bash?

The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.

How do you replace a word in a string in shell script?

To replace a substring with new value in a string in Bash Script, we can use sed command. sed stands for stream editor and can be used for find and replace operation. We can specify to sed command whether to replace the first occurrence or all occurrences of the substring in the string.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

Try this:

$ echo oneone oneone $ !!:gs/one/two/    # Repeats last command; substitutes 'one' --> 'two'. twotwo 
like image 151
John Feminella Avatar answered Oct 19 '22 11:10

John Feminella