[comp.unix.shell] Problems with variable substitution in C-Shells

lim@ecs.umass.edu (11/30/90)

Hi,

I'd appreciate any answers you may have to the following simple(?) question. It 
concerns variable substitution in a C-Shell:

Suppose I have:
@ num = 5
set str = num
alias ech 'echo $\!*'

So:
(ech str) will output num, but (ech $str) won't output 5. The error message 
says: variable syntax.

In other words:
$str = num, but how may I do something like $($str) ?


Thanks in advance.


Jonathan Lim

maart@cs.vu.nl (Maarten Litmaath) (12/04/90)

In article <11585.27563778@ecs.umass.edu>,
	lim@ecs.umass.edu writes:
)...
)Suppose I have:
)@ num = 5
)set str = num
)alias ech 'echo $\!*'
)
)So:
)(ech str) will output num, but (ech $str) won't output 5. The error message 
)says: variable syntax.

alias ech 'eval echo \$\!*'
--
Q: "Is there a newsgroup for astrology/para-normal?"
A: "Why don't you consult the stars and find out ??"

	--  Phil Watson <pwatson@sunb.mqcc.mq.oz.au>

roger@wrs.com (Roger Rohrbach) (12/11/90)

lim@ecs.umass.edu writes:

>Suppose I have:
>@ num = 5
>set str = num
>alias ech 'echo $\!*'
>So:
>(ech str) will output num, but (ech $str) won't output 5.
>In other words:
>$str = num, but how may I do something like $($str) ?


  Another job for "eval"!  The following:

    alias ech 'eval echo \$\!*'

will do what you want.  The idea here is to build the command:

    echo $num

that will echo the desired value, and then to execute it.  (This is known
as dynamic programming; Lisp programmers are big fans of this kind of thing).
So, we escape the first "$", and the shell will substitute "$str" for "\!*",
giving:

    eval echo \$num

and eval will strip off the protective "\", yielding the desire result.

-- 
Roger Rohrbach                                  sun!wrs!roger    roger@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "Two men need one money, but one money needs no man."               |