Home > other >  Exporting multiple variables in a for loop
Exporting multiple variables in a for loop

Time:02-10

I am having trouble exporting multiple env variables in a for loop of a bash script.

The full script looks something like:

IFS_old="$IFS"
IFS=';'
test_data="secret1='data1';secret2='data2';secret3='data3'"
for secret in $test_data
do
    eval "export $secret"
done
IFS="$IFS_old"

So the input string consists of variables and their values separated by a semicolon. I loop through it and want to export each of them; however, the variables that get exported have "null" values.

I also tried composing a full command string in a separate variable, and executing it both via bash -c "$command" and eval "$command" statements, but it didn't work.

Can you please help me figure out what's wrong?

CodePudding user response:

This works fine. The variables exported are local and only available until the end of your script. When the script closes they are no longer available in the shell. This is normal. if you write bash on the last line of your script it will open a new terminal with the exported variables available.

  • Related