On Thu, Sep 19, 2024 at 10:03:01 -0400, Steve Matzura wrote:
sed -i 's/replace-this/$with_this_variable/'
The simplistic answer is "you want double quotes instead of single quotes".
sed -i "s/replace-this/$with_this_variable/"
However, the problem goes a bit deeper than that. Sed *always* uses
regular expressions to match things. There is no option to use a
plain string. Therefore, if your replace-this has any special characters
in it (or has the *possibility* of it, e.g. if it's a variable as well),
then you may get unpredictable results.
(Slashes or newlines in the $with_this_variable content would also cause
an issue.)
<https://mywiki.wooledge.org/BashFAQ/021> suggests using perl instead:
in="$search" out="$replace" perl -pi -e 's/\Q$ENV{"in"}/$ENV{"out"}/g' ./*
This forces a plain string match for the "in" side, so it'll work no
matter what characters are used.
There are other examples given on that page as well.