Sujet : Re: create variables in a loop?
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.unix.shellDate : 29. May 2024, 13:09:31
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <v375tr$1iinj$1@news.xmission.com>
References : 1
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <
lbog03FfbrqU1@mid.individual.net>,
Dr Eberhard W Lisse <
nospam@lisse.NA> wrote:
Hi,
>
I would like to do something like
>
for i in USD GBP
do
$i=somevalue
done
>
does not work, of course.
The general answer is to use "eval", so something like:
for i in USD GBP
do
eval $i=somevalue
done
But this doesn't scale well if "somevalue" is other than a simple string.
(But see below...)
Alternatively, with bash, you can use the "nameref" capability - see the
man page for details.
Update to add: this actually works:
for i in one two three;do eval $i='"$(fortune)"';done
-- "Every time Mitt opens his mouth, a swing state gets its wings."(Should be on a bumper sticker)