Problems with variable substitution in C-Shells

Roger Rohrbach roger at wrs.com
Tue Dec 11 20:35:31 AEST 1990


lim at 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 at wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "Two men need one money, but one money needs no man."               |



More information about the Comp.unix.shell mailing list