It is currently Thu, 20 Jan 2022 05:17:45 GMT



 
Author Message
 HELP! Double variable expansion question
I've got a variable expansion question that's got everyone in my shop
stumpped!  I'm using ksh and want to do a variable expansion of a variable
expansion - preferably on one line.

eg.  I want something like this:

z=${ecs_"$z1"_src}

given that z1="deve" and ecs_deve_src="ecs/deve/src"

then `echo $z` should return "ecs/deve/src".

Is there a simple way of doing this?

Thanks, Allan Dong



 Wed, 25 Oct 1995 06:03:30 GMT   
 HELP! Double variable expansion question
ad...@galaxy.gov.bc.ca wrote in <1993May7.150330.2...@galaxy.gov.bc.ca>:

| I've got a variable expansion question that's got everyone in my shop
| stumpped!  I'm using ksh and want to do a variable expansion of a variable
| expansion - preferably on one line.
|
| eg.  I want something like this:
|
| z=${ecs_"$z1"_src}
|
| given that z1="deve" and ecs_deve_src="ecs/deve/src"
|
| then `echo $z` should return "ecs/deve/src".
|
| Is there a simple way of doing this?

Uh, yes.  The following works in both sh and ksh:

z1=deve
ecs_deve_src=ecs/deve/src
eval z=\${ecs_"$z1"_src} # or eval z=\${ecs_${z1}_src}
                         # or eval z=\$ecs_${z1}_src
                         # or eval z=\$ecs_"$z1"_src
echo $z

eval [not by definition but in effect] makes the shell scan a line twice.
Because one dollar sign is escaped, it isn't interpreted until the second
pass, but the unescaped dollar sign is interpreted on the first pass.

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297
datt...@genesis.mcs.com   CompuServe: 73720,1570   MCI Mail: 426-1818



 Wed, 25 Oct 1995 13:46:42 GMT   
 
   [ 2 post ] 

Similar Threads

1. double quote variable expansion

2. variable expansion: weird question

3. variable expansion: simple question

4. Double Variables in a Shell Script - Help Please

5. double substitution (expansion)

6. Double expansion in csh

7. Help on Double Variables ...

8. HELP:bash: making tilde expansion after command expansion...

9. tilde + variable expansion in bash

10. autoconf variable expansion


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software