[gnu-sed-devel] Re: & commercial replace problems

On Tue, Mar 21, 2017 at 3:11 AM, Mickael MONSIEUR
> I have a problem with the replace when a "&" commercial is present.
> Because "&" it is used in URL.  (page.php?hello=world&john=doe)
> Example:
> echo "" > test
> APIKEY="test&test&test"
> sed  -i -e "s//$APIKEY/g" test
> cat test
> rm test
> Result:
> root@local:/tmp# ./test.sh
> testtesttest
> The replacement text (search) should not be totally insensitive to regular
> expressions?

"&" has a special meaning in the right hand side of a sed replacement
expression (it expands to the text matched by the regexp in the LHS).
If you want a literal ampersand ("&"), you should quote it. Compare
the following.

  # Using unescaped "&" on the RHS, it is replaced by "b":
  $ echo abc | sed 's/b/b&/'

  # Escape it, and you get the literal:
  $ echo abc | sed 's/b/b\&/'

This is well documented in the manual:

This message from: http://www.mailbrowse.com/gnu-sed-devel/248.html
Previous message: Patch for sed- on mingw-w64
Next message:Suggestion: tests: refactor tests to new init.sh-style shell scripts