Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exporting multiple variables in a for loop

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading