It is currently Mon, 08 Aug 2022 19:09:59 GMT



 
Author Message
 shell scripting: echoing contents of contents of variable
Hello,

Consider the following short example:

$ a="foo"
$ b="a"
$ echo $b
a
$ echo $$b
9540b

I was expecting "foo". Any ideas on how I could get the expected result?

Thanks,

Neil



 Thu, 21 Sep 2006 11:30:17 GMT   
 shell scripting: echoing contents of contents of variable

Neil Zanella <nzane...@cs.mun.ca>:

stefan2@platon:~/test> a="foo"
stefan2@platon:~/test> b="$a"
stefan2@platon:~/test> echo $a
foo
stefan2@platon:~/test> echo $b
foo



 Thu, 21 Sep 2006 12:38:20 GMT   
 shell scripting: echoing contents of contents of variable

Try

% eval echo '$'$b

The shell is interpreting $$ as the process ID.

--
 __ Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Sit loosely in the saddle of life.
    -- Robert Louis Stevenson



 Thu, 21 Sep 2006 13:02:13 GMT   
 shell scripting: echoing contents of contents of variable

echo ${!b}

man bash
man ksh

--
William Park, Open Geometry Consulting, <opengeome...@yahoo.ca>
Linux solution for data processing and document management.



 Thu, 21 Sep 2006 14:42:40 GMT   
 shell scripting: echoing contents of contents of variable
2004-04-4, 06:42(+00), William Park:

Only newer versions of bash. Not with ksh. With ksh93, you can
use name references:

typeset -n b=a
echo $b

${!b} would then expand to "a", not to the content (splitted and
expanded) of $a.

With zsh:

echo ${(P)a}

eval "echo \"\$$a\""
works with every shell.

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]



 Thu, 21 Sep 2006 16:32:01 GMT   
 shell scripting: echoing contents of contents of variable

In case it was not obvious, I meant to ask about how to do it without changing
the first two lines. Otherwise the answer is quite simple...

Erik Max Francis <m...@alcyone.com> wrote in message:

That works. I guess that's what eval is for!?

That works too.

BTW, which of these two constructs also work in traditional System V sh shell?
It seems like there are two ways of doing the same thing. One of them ought to
be more portable across shells and hence preferable. Which one?

Thanks,

Neil



 Thu, 21 Sep 2006 19:29:26 GMT   
 shell scripting: echoing contents of contents of variable
2004-04-4, 04:29(-07), Neil Zanella:
[...]

eval concatenates its arguments with spaces and has the shell
evaluate that string as shell code.

Here, it takes "echo" and "$" followed by as many arguments as
$b expands to (most likely "a" here unless you modified IFS,
putting $b insite quotes would have been more correct).

The result is "echo $a". Then the shell evaluates that string.
(runs echo with the list of arguments resulting from the
expansion of $a).

${!var} is bash specific. It's a syntax error in System V sh or
ksh or ash or zsh and has a different meaning in ksh93.

eval was already in Unix V7 Bourne shell (1975). There's also
one in csh.

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]



 Thu, 21 Sep 2006 21:58:37 GMT   
 
   [ 7 post ] 

Similar Threads

1. file contents into variable, or global variable from shell script

2. Executing contents of variable in shell script

3. How do i transfer the contents of shell variable to a variable used in C program

4. Help: Shell variable content stripping !

5. file contents into shell/env variable

6. Listing contents of shell variable thats = a directory path

7. Q: getting variable contents in bourne shell

8. shell script to compare the content of two file

9. csh shell, problem with echoing variable variables...

10. Help about the diff btw MIME's Content-ID and Content-Location header


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