MOHAMED MOHAMED - 1 year ago 71
Bash Question

Remove the all occurences of a word from a string in bash shell linux

I have the following string

str="toto1 toto2 toto3 toto4 toto2 toto5"

occurs twice in the string. How can I remove all
occurrences from the string?

I tried this:

echo ${str/toto2/}

But this will remove only the first occurence of

toto1 toto3 toto4 toto2 toto5 # output

How can I remove all
occurences from the string?

Answer Source

Found it. the solution is:

echo ${str//toto2/}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download