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
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
Quote:> Consider the following short example:
> $ a="foo"
> $ b="a"
> $ echo $b
> a
> $ a="foo"
> $ b="a"
> $ echo $b
> a
> $ echo $$b
> 9540b
> I was expecting "foo". Any ideas on how I could get the expected
> result?
% 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
> 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?
man bash
man ksh
--
Linux solution for data processing and document management.
>> 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
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"]
> foo
> foo
That works. I guess that's what eval is for!?Quote:> $ eval echo '$'$b
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
eval concatenates its arguments with spaces and has the shellQuote:>> $ eval echo '$'$b
> That works. I guess that's what eval is for!?
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 orQuote:>> 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?
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
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