If I run these commands from a script:
#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla
it is fine.
But, if I run:
#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./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?
set -x
in the shell to get the shell to echo each command just before it executes them. This can clear up a lot of confusion. (Also, I often useset -u
to make de-referencing unset variables a hard error. (Seeset -e
too.)) - bobbogo