MOHAMED MOHAMED - 4 months ago 12
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"


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

I tried this:

echo ${str/toto2/}


But this will remove only the first occurence of
toto2
.

toto1 toto3 toto4 toto2 toto5 # output


How can I remove all
toto2
occurences from the string?

Answer

Found it. the solution is:

echo ${str//toto2/}