Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash export variables received lines by lines

Tags:

bash

I got a simple problem but very boring.

The goal is to write a shell script that run on EC2 instance to exports tags for rest of the script... Something like:

ec2-describe-tags [...]
    | while IFS=':' read name value; do
        export "$name"="$value"
    done

Not so uggly but don't work, of course cause the export is in the while loop, executed in pipe.

My question is: how to write this correctly? Of course I cannot predict names nor numbers of received tags.

like image 356
farvilain Avatar asked May 09 '26 06:05

farvilain


1 Answers

Try this:

while IFS=: read name value; do
    export $name="$value"
done < <(ec2-describe-instance ...)

A pipeline runs the commands in a subshell, so the variables don't persist when it's done.

like image 123
Barmar Avatar answered May 11 '26 02:05

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!