I am trying to set environment variables in a bash script to be read by another bash script, but they are not getting set properly. I am on Ubuntu 20.04.
setting environment variables in a script:
setenv.env
export DB1_IMAGE="postgres:latest"
run it: . setenv.env
test it: echo $DB1_IMAGE
result: postgres:latest
script to test the environment variable value:
test.sh
#!/bin/bash
echo $DB1_IMAGE
if [[ $DB1_IMAGE == "postgres:latest" ]]
then
echo "equals"
else
echo "not equals"
fi
run the test script: . test.sh
result:
postgres:latest
not equals
now set the environment variable with command line:
export DB1_IMAGE="postgres:latest"
now run the test script again: . test.sh
result:
postgres:latest
equals
Summary: When an environment variable is set with a bash script, that value will fail an equals comparison in another bash script. When that same environment variable is set with a command line, it passes the equals test. I can't explain why this is. I feel like I'm missing something obvious. How could the == test fail? Are there unprintable characters being inserted somehow? Please help..