Thread View: comp.unix.shell
3 messages
3 total messages
Started by lim
Fri, 30 Nov 1990 10:42
Problems with variable substitution in C-Shells
Author: lim
Date: Fri, 30 Nov 1990 10:42
Date: Fri, 30 Nov 1990 10:42
22 lines
756 bytes
756 bytes
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
Re: Problems with variable substitution in C-Shells
Author: Maarten Litmaath
Date: Mon, 03 Dec 1990 19:33
Date: Mon, 03 Dec 1990 19:33
18 lines
959 bytes
959 bytes
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>
Re: Problems with variable substitution in C-Shells
Author: Roger Rohrbach
Date: Tue, 11 Dec 1990 09:35
Date: Tue, 11 Dec 1990 09:35
34 lines
1479 bytes
1479 bytes
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." |
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads