Sujet : Re: create variables in a loop?
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shellDate : 29. May 2024, 14:36:38
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83ed9k94mh.fsf@helmutwaitzmann.news.arcor.de>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Dr Eberhard W Lisse <
nospam@lisse.NA>:
Hi, >
>
I would like to do something like >
>
for i in USD GBP
do
$i=somevalue
done
>
does not work, of course. >
>
Any idea on how to create (and fill variables through a loop)? >
Yes.
for i in USD GBP
do
somevalue=... &&
eval "$i"'="$somevalue"'
done
But note, that variable names consisting of only upper case letters, digits, and underscores are by convention reserved for the environment. Therefore I recommend to do for i in USD GBP
do
somevalue=... &&
eval 'currency_'"$i"'="$somevalue"'
done
instead, to get variables like currency_USD, currency_GBP, and so on.