Issue
does bash support multiple strings replacement on a variable at once?
on a variable (example V variable) I want to replace XXXXXXX with a string1, YYYYY with a string2 and ZZZZZZZ with a string3
Is it possible to run 1 such relpacement command instead of the 3 run below?
$ V="AAAAAAA/XXXXXXX/BBBBBB/YYYYY/CCCCCC/ZZZZZZZ"
$ V=${V//XXXXXXX/string1}
$ V=${V//YYYYY/string2}
$ V=${V//ZZZZZZZ/string3}
$ echo $V
AAAAAAA/string1/BBBBBB/string2/CCCCCC/string3
$
Solution
Yes you can do it like below:-
V=$(echo "$V" | sed 's/XXXXXXX/string1/g;s/YYYYY/string2/g;s/ZZZZZZZ/string3/g')
How will it work? sed
command will search for sting XXXXXX and replace it with string2, replace string YYYYYY with string2 and finally replace ZZZZZZZ with string3 and store the new string into variable V.
So now check the result like below:-
echo $V
Answered By - Abhijit Pritam Dutta Answer Checked By - Katrina (WPSolving Volunteer)