Environment variable substitution in sed

If I run these commands from a script:

#my.sh
PWD=bla
sed 's/xxx/'$PWD"https://stackoverflow.com/"
...
$ ./my.sh
xxx
bla

it is fine.

But, if I run:

#my.sh
sed 's/xxx/'$PWD"https://stackoverflow.com/"
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown option to `s' 

I read in tutorials that to substitute environment variables from shell you need to stop, and ‘out quote’ the $varname part so that it is not substituted directly, which is what I did, and which works only if the variable is defined immediately before.

How can I get sed to recognize a $var as an environment variable as it is defined in the shell?

11 Answers
11

Leave a Comment