Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

3

1 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"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy