Sorry, easy question, but I can't find how to do it: I have a special char "&" in a Variables that I have to use in sed.
Example:
#The template .env
cat .env
MY_SECRET = VAR_MY_SECRET
export MY_SECRET="2C&ga&8M&jy3&g&WE&U"
sed -i "s|VAR_MY_SECRET|$MY_SECRET|g" .env
The new .env file
cat .env
MY_SECRET = 2CVAR_MY_SECRETgaVAR_MY_SECRET8MVAR_MY_SECRETjy3VAR_MY_SECRETgVAR_MY_SECRETWEVAR_MY_SECRETU
31 Answer
The comment from @Gordon Davisson should help.
Some applications that handle regular expressions support quote start \Q and quote end \E which makes it much easier to escape meta-characters. Perl - if available - handles quote start and quote end like this:
echo "MY_SECRET = \"VAR_MY_SECRET\"" | perl -pe "s|VAR_MY_SECRET|\Q$MY_SECRET\E|g" > .env
cat .env
MY_SECRET = "2C\&ga\&8M\&jy3\&g\&WE\&U"