macoscommand-lineterminalenv-file

MacOS export of env var is being escaped or misinterpreted on shell


Mac OS here. On the terminal, I create the following .env file:

export FIZZ=foo
export BUZZ="$2a$10$Hk1PB6Eyf5Pu71JLfH6fCexjzOIwkctk.pQJ4oYWP.m4qdRKRQlyO"

Then I run source .env && echo $FIZZ and I see:

foo

So far, so good. But now I run echo $BUZZ and the output is:

a0.pQJ4oYWP.m4qdRKRQlyO

I explicitly put the value for BUZZ in double quotes ("$2a$10$Hk1PB6Eyf5Pu71JLfH6fCexjzOIwkctk.pQJ4oYWP.m4qdRKRQlyO"), so why is it outputting as "a0.pQJ4oYWP.m4qdRKRQlyO"?


Solution

  • I was able to reproduce what you saw. I'm pretty sure you're having issues with the environment variable substitution that bash does. (triggered by the "$" character.) I know of two ways to "fix" it.

    One is to escape the "$" characters with a preceding backslash. export BUZZ=\$2a\$10\$Hk1PB6Eyf5Pu71JLfH6fCexjzOIwkctk.pQJ4oYWP.m4qdRKRQlyO

    Another is to change the way you quote the string. Use single quotes instead of double quotes... export BUZZ='$2a$10$Hk1PB6Eyf5Pu71JLfH6fCexjzOIwkctk.pQJ4oYWP.m4qdRKRQlyO'.

    The double quotes allow environment variable substitution to continue, the single quotes prevent that from occurring.