Why can’t the bash script export variables

bashenvironment-variables

I am using MSYS and have a file vars.txt with variable, value keys like:

WINDIR C:/WINDOWS
STS_BUILD_DIRECTORY D:/STS/TMP
ALLUSERSPROFILE C:/Documents and Settings/All Users

I want to read this in and set environment variables up. I have a bash script setenv:

while read var value;
do
  echo "performing export $var=$value"
  export $var='$value'
done 

and I call it with

cat vars.txt | source setenv

However in my environment the variables are not set. I also tried making it into a function but no joy. Anybody here know what I'm doign wrong? Thanks.

Best Answer

The pipe sets up a subshell. When the subshell exits, the variables are lost.

Try this:

source setenv < vars.txt

Also your single quotes may prevent the expansion of the variable (I don't know if this is true in MSYS). Try changing the export line to this:

export $var="$value"

You can use declare instead of export if the variables don't need to be exported.

Related Question