nullByteMe
nullByteMe

Reputation: 6391

How do I read a multiline variable one line at a time?

I have a variable that we can call largeVar and has the following content:

var1="blah1"
var2="blah2"
var3="blah3"

So if I do a printf "$largeVar" I get the exact content above. I need to, however, add these vars to my environment without creating a property file. So I'm trying to do this:

for var in "$largeVar"
do 
   eval "$var"
done

But this is just eval the $largeVar and not each line. So I then tried this:

while cat "$largeVar" | read var
do
   printf "$var"
done

This seems to work... kinda (I only tested with printf to see what would happen) but the final line says echo : File name too long

Is there anyway to do what I'm trying to do?

Upvotes: 2

Views: 105

Answers (1)

Mike Pelley
Mike Pelley

Reputation: 3116

You can just do eval $largeVar as environment variables can be separated with whitespace in bash.

Upvotes: 2

Related Questions