Found the solution is that, In a recent shell this could be done like,
Need to put this //&/\\&
static set of character to solve this ampersand (&) problem with sed
s1="XXX&XXX"
sed "s/ww/${s1//&/\\&}/g" <<< 'this is ww'
this is XXX&XXX
Other situation,
s2="&xx&xx&x&"
sed "s/ww/${s2//&/\\&}/g" <<< 'this is ww'
this is &xx&xx&x&
A quick search revealed the culprit;
The REPLACEMENT can contain \N (N being a number from 1 to 9, inclusive)
references, which refer to the portion of the match which is contained
between the Nth \( and its matching \). Also, the REPLACEMENT can contain
unescaped & characters which reference the whole matched portion of the pattern space
Let’s try with awk
URI='https://user:pass@example.com/?num=42&type=magic'
$ echo 'my_link = %link%' | awk "{ gsub(/%link%/, \"$URI\"); print }"
my_link = https://user:pass@example.com/?num=42%link%type=magic
Find more detail on https://www.gnu.org/software/sed/manual/sed.html
Cheers!