Here is the example:
v="1 & 2"
x="3 & 4"
echo $v | sed "s/$v/$x/"
3 1 & 2 4 <------result not replaced by 3 & 4
I want to replace 1 & 2 to 3 & 4, but seem the result given not the one I needs.
Anyone can help to fix my problem?
Here is the example:
v="1 & 2"
x="3 & 4"
echo $v | sed "s/$v/$x/"
3 1 & 2 4 <------result not replaced by 3 & 4
I want to replace 1 & 2 to 3 & 4, but seem the result given not the one I needs.
Anyone can help to fix my problem?
In sed & has a special meaning. It contains the entire match from the substitution part. When your replacement variable is expanded sed sees it as 3 & 4 where & contains 1 & 2. Hence you get 3 1 & 2 4 in the output.
You can escape the & to make it work.
$ v="1 & 2"
$ x="3 \& 4"
$ echo $v | sed "s/$v/$x/"
3 & 4