shell scripting: echoing contents of contents of variable

shell scripting: echoing contents of contents of variable

Post by Neil Zanel » Mon, 05 Apr 2004 12:30:17



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

 
 
 

shell scripting: echoing contents of contents of variable

Post by Stefan Lagotzk » Mon, 05 Apr 2004 13:38:20



Quote:> Consider the following short example:

> $ a="foo"
> $ b="a"
> $ echo $b
> a




foo

foo

 
 
 

shell scripting: echoing contents of contents of variable

Post by Erik Max Franci » Mon, 05 Apr 2004 14:02:13



> 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?

Try

% eval echo '$'$b

The shell is interpreting $$ as the process ID.

--

/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Sit loosely in the saddle of life.
    -- Robert Louis Stevenson

 
 
 

shell scripting: echoing contents of contents of variable

Post by William Par » Mon, 05 Apr 2004 15:42:40



> 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?

echo ${!b}

man bash
man ksh

--

Linux solution for data processing and document management.

 
 
 

shell scripting: echoing contents of contents of variable

Post by Stephane CHAZELA » Mon, 05 Apr 2004 17:32:01


2004-04-4, 06:42(+00), William Park:


>> 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?

> echo ${!b}

> man bash
> man ksh

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"]

 
 
 

shell scripting: echoing contents of contents of variable

Post by Neil Zanel » Mon, 05 Apr 2004 20:29:26



> > $ a="foo"
> > $ b="a"
> > $ echo $b
> > a




> foo

> foo

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...


Quote:> $ eval echo '$'$b

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


> echo ${!b}

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

 
 
 

shell scripting: echoing contents of contents of variable

Post by Stephane CHAZELA » Mon, 05 Apr 2004 22:58:37


2004-04-4, 04:29(-07), Neil Zanella:
[...]

Quote:>> $ eval echo '$'$b

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

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).

Quote:>> echo ${!b}

> 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?

${!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"]

 
 
 

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

I want to maintain a filename across editing sessions (i access my
editor through csh shell scripts).  I have two choices

1) shell script writes filename to file, reads it later.  This requires
a shell command to read first word of a file into a variable - I don't
know how to do this - can you help?

2) shell script defines global variable as filename.  I don't know how
to do this either (the variable goes away when the script completes).

Any suggestions appreciated, please use email

thanks

2. Suggestionf for C++ compiler to be used with Sunsoft Interactive Unix

3. Executing contents of variable in shell script

4. Verite Support in X???

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

6. Netscape kills taskbar in FVWM95-2?

7. file contents into shell/env variable

8. newbie: newbie: login problem with Linux Red Hat on IBM A20

9. Help: Shell variable content stripping !

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

11. Q: getting variable contents in bourne shell

12. shell script to compare the content of two file

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