Csh question

Richard A. O'Keefe ok at quintus.uucp
Thu Jul 21 08:50:12 AEST 1988


In article <8807191241.AA18881 at decwrl.dec.com> ellis at ultra.dec.com (David Ellis) writes:
>Is there any way to get the value of a variable whose name is the value of
>another variable?
>e.g. if we've done set x=a and set a=3, is there any simple csh expression
>in terms of x that yields the value of a (3)?

One method is to use "eval".  E.g.
	% set x=a a=3
	% eval echo '$'$x
prints	3

Another method is to exploit the fact that the "set" command with
no arguments prints out the shell variables in alphabetic order,
one per line, in the form "<name><TAB><value>".  So you can do
	% set | sed -n -e "s/^$x<TAB>//p"
where <TAB> is to be replaced by an actual tab character.

Rather than using
	... `eval echo '$'$x` ...
it may be more efficient to use
	eval set y='$'$x
	... $y ...
as that doesn't involve forking another shell for the `` form.

The Bourne shell also has 'eval'
	$ x=a a=3
	$ eval y='$'$x
	$ eval echo '$'$x
sets y to 3 and prints 3.



More information about the Comp.unix.questions mailing list