[comp.unix.questions] indirect shell-vars

ruud@targon.UUCP (Ruud Harmsen) (09/11/89)

I'm looking for a way to reference a variable in the Bourne-shell,
whose name is given in another variable.  I found the following solution
using a sub-shell myself, but it seems rather clumsy:.

export x1; x1=one
export x2; x2=two
echo which var\?
read xx
sh -c "echo \$x`echo $xx`"

Are there any better/faster/shorter ways?  Perhaps using "eval", but
also executes the string, which is too much.

ok@cs.mu.oz.au (Richard O'Keefe) (09/12/89)

In article <627@targon.UUCP>, ruud@targon.UUCP (Ruud Harmsen) writes:
> I'm looking for a way to reference a variable in the Bourne-shell,
> whose name is given in another variable. ... Perhaps using "eval", but
> also executes the string, which is too much.

	x="foo <baz> ugh"
	y=x
Method 1:
	eval 'z="${'$y'}"'
Method 2:
	cmd="echo \"\${$y}\""
	z=`eval $cmd`
They're not perfect, mind.

cpcahil@virtech.UUCP (Conor P. Cahill) (09/13/89)

In article <627@targon.UUCP>, ruud@targon.UUCP (Ruud Harmsen) writes:
> 
> sh -c "echo \$x`echo $xx`"
> 
> Are there any better/faster/shorter ways?  Perhaps using "eval", but
> also executes the string, which is too much.

How about:

eval echo \$$xx



-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

karish@forel.stanford.edu (Chuck Karish) (09/13/89)

In article <627@targon.UUCP> ruud@targon.UUCP (Ruud Harmsen) wrote:
 >I'm looking for a way to reference a variable in the Bourne-shell,
 >whose name is given in another variable.  I found the following solution
 >using a sub-shell myself, but it seems rather clumsy:.

 >export x1; x1=one
 >export x2; x2=two
 >echo which var\?
 >read xx
 >sh -c "echo \$x`echo $xx`"

eval echo \$x$xx

By escaping some of the `$' signs, you can cause eval to expand the
variables in the desired order.

	Chuck Karish		karish@mindcraft.com
	(415) 493-9000		karish@forel.stanford.edu