help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

looping over variables & exporting them at the same time ?


From: heavy . gear5130
Subject: looping over variables & exporting them at the same time ?
Date: Sat, 27 Jan 2024 08:22:17 -0500
User-agent: Cyrus-JMAP/3.11.0-alpha0-119-ga8b98d1bd8-fm-20240108.001-ga8b98d1b

Hello,

I have two variables defined in my env.

        echo $A; echo $B

                this is a test xtest of A str
                this is a test xtest of B str

I want to loop over the varable names, A & B, editing the strings and exporting 
the values.

One at a time, without a loop, I would do

        export A=$( echo "$A" | sed -e 's| xtest | |g' )
        export B=$( echo "$B" | sed -e 's| xtest | |g' )

So after,

        echo $A; echo $B

                this is a test of A str
                this is a test of B str

I'd like to do something like this, using a loop, instead

        for vars in A B
        do
          export $vars=$( echo $vars | sed -e 's| xtest | |g' )
        done

If I exec *that* loop, I get no error, but after, just

        echo $A; echo $B

                A
                B

I guess I need some sort of indirection or escaping ?

What's the right syntax to use for that loop, so that I end up with the same

        echo $A; echo $B

                this is a test of A str
                this is a test of B str

?

Thanks a lot for any help!

Dave J.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]