It is currently Tue, 17 May 2022 02:54:13 GMT



 
Author Message
 variable expansion: simple question
I run the following:

#! /bin/sh
    test01=/foo/bar
    cat=test
    dog=01
    var1=$test01
    var2=$cat$dog
    echo $var1 $var2

I get
/foo/bar test01

How do I make the shell to echo

$ of ($cat$dog)=$ of (test01)=/foo/bar ?

Thanks in advance.

Surjit



 Fri, 19 Oct 2001 03:00:00 GMT   
 variable expansion: simple question
In article <372DFAB6.9C5FA...@hnc.com>, Surjit Singh  <s...@hnc.com> wrote:

I don't understand your last line, but I suspect you need to use eval.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



 Fri, 19 Oct 2001 03:00:00 GMT   
 variable expansion: simple question

He *is* rather cryptic, no?  I *think* what he wants is var2 to have
the value of $test01 by parsing $cat and $dog to "test01" and then
evaluating that test01 so that $var2 winds up being /foo/bar.

And, of course, eval will do that quite nicely.  Instead of

var2=$cat$dog

do

eval var2=\$$cat$dog

and you get the double dereferencing you want.

                       Chris Mattern



 Sat, 20 Oct 2001 03:00:00 GMT   
 variable expansion: simple question

 Here's a ksh solution (probably could be whittled down):

#!/usr/bin/ksh
# want output to be:
# $ of ($cat$dog)=$ of (test01)=/foo/bar

test01=/foo/bar
cat=test
dog=01
var1=$test01
var2=\$cat\$dog    # Note var2 is different from above

_var2=$(eval print $var2)
__var2=$(eval print \${$(print $_var2)})
print "\$ of ($var2)=\$ of ($_var2)=$__var2"

Zartaj



 Sat, 20 Oct 2001 03:00:00 GMT   
 variable expansion: simple question

  Looking at Christopher Mattern's suggestion, here's some whittling:
By the way it would help if you could describe what you want the output
to look like in terms of the variables and not their values. E.g. I want
to
print out the value of the variable represented by the value of var2
etc.

#!/usr/bin/ksh
# want output to be:
# $ of ($cat$dog)=$ of (test01)=/foo/bar

test01=/foo/bar
cat=test
dog=01
var1=$test01
var2='$cat$dog'

eval _var2=$var2
eval __var2=\$$_var2
print "\$ of ($var2)=\$ of ($_var2)=$__var2"

Zartaj



 Sat, 20 Oct 2001 03:00:00 GMT   
 variable expansion: simple question
Thank you very much. eval worked.

Surjit Singh



 Sat, 20 Oct 2001 03:00:00 GMT   
 
   [ 7 post ] 

Similar Threads

1. simple bash alias expansion question

2. variable expansion: weird question

3. HELP! Double variable expansion question

4. simple shell script variable question

5. A simple question about left-margin variable in emacs

6. how to ++ my variable? simple question easy awnser

7. tilde + variable expansion in bash

8. autoconf variable expansion

9. Variable and Pathname Expansion

10. Variable Expansion With sed


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