Sujet : How to use "eval" (was: create variables in a loop?)
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shellDate : 29. May 2024, 15:01:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83bk4o93gh.fsf_-_@helmutwaitzmann.news.arcor.de>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
gazelle@shell.xmission.com (Kenny McCormack):
Dr Eberhard W Lisse <nospam@lisse.NA> wrote:
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. >
First assign "somevalue" to a variable, not using "eval". Then assign that variable's value to the variable named by "$i", using "eval" somevariablename=... &&
# Note, that in the following assignment "..." may be any
# valid shell expression expanding to a string, not just
# a simple string:
#
somevalue=... &&
# Note, how the apostrophes are placed around the right
# hand part of the following assignment to be "eval"ed
# but not around the name of the receiving variable to be
# assigned to:
#
eval "$somevariablename"'="$somevalue"'