${${var}}

${${var}}

am 07.11.2007 16:26:36 von Anoop kumar V

How can I get the value of the value of a variable. I tried using
$"$var and of course $$var ($$ gives the pid) wouldnt work.

I also tried $"${var}" - none worked and i get the error bad
substitution. I could somewhat make it to work with eval and echo and
`` (back quotes) and all. But I am sure there is a simpler way. Also I
am using this as variables to generate a long path for find etc., that
I would like to keep it smaller.

Thanks.

Re: ${${var}}

am 07.11.2007 16:46:57 von Tiago Peczenyj

On Nov 7, 1:26 pm, Anoop wrote:
> How can I get the value of the value of a variable. I tried using
> $"$var and of course $$var ($$ gives the pid) wouldnt work.
>
> I also tried $"${var}" - none worked and i get the error bad
> substitution. I could somewhat make it to work with eval and echo and
> `` (back quotes) and all. But I am sure there is a simpler way. Also I
> am using this as variables to generate a long path for find etc., that
> I would like to keep it smaller.
>
> Thanks.

use eval:

THANKS="best regards"
VARIABLE=THANKS

$ echo $VARIABLE
THANKS

$ echo \$$VARIABLE
$THANKS

$ eval echo \$$VARIABLE
best regards

Re: ${${var}}

am 07.11.2007 16:55:40 von Lew Pitcher

On Nov 7, 10:26 am, Anoop wrote:
> How can I get the value of the value of a variable. I tried using
> $"$var and of course $$var ($$ gives the pid) wouldnt work.

In bash, you can use the ${!var} to perform an indirect expansion

For example:
lpitcher@merlin:~$ abc=1

lpitcher@merlin:~$ def=abc

lpitcher@merlin:~$ echo $abc
1

lpitcher@merlin:~$ echo $def
abc

lpitcher@merlin:~$ echo ${!def}
1

lpitcher@merlin:~$

HTH
--
Lew

Re: ${${var}}

am 07.11.2007 16:56:52 von Loki Harfagr

On Wed, 07 Nov 2007 15:26:36 +0000, Anoop wrote:

> How can I get the value of the value of a variable. I tried using $"$var
> and of course $$var ($$ gives the pid) wouldnt work.
>
> I also tried $"${var}" - none worked and i get the error bad
> substitution. I could somewhat make it to work with eval and echo and ``
> (back quotes) and all. But I am sure there is a simpler way. Also I am
> using this as variables to generate a long path for find etc., that I
> would like to keep it smaller.
>

This is indirect referencing, you have two flavors:
- the old and smokey:-O
# eval echo \$$VAR

- the new and shiny (V2+) ;-)
# echo ${!VAR}

You may like to read the Advanced Bash-Scripting Guide
pages about it, the ones on the topic are
mainly:
http://tldp.org/LDP/abs/html/ivr.html
and
http://tldp.org/LDP/abs/html/bashver2.html

Re: ${${var}}

am 07.11.2007 17:41:16 von Stephane CHAZELAS

2007-11-07, 15:56(+00), loki harfagr:
> On Wed, 07 Nov 2007 15:26:36 +0000, Anoop wrote:
>
>> How can I get the value of the value of a variable. I tried using $"$var
>> and of course $$var ($$ gives the pid) wouldnt work.
>>
>> I also tried $"${var}" - none worked and i get the error bad
>> substitution. I could somewhat make it to work with eval and echo and ``
>> (back quotes) and all. But I am sure there is a simpler way. Also I am
>> using this as variables to generate a long path for find etc., that I
>> would like to keep it smaller.
>>
>
> This is indirect referencing, you have two flavors:
> - the old and smokey:-O
> # eval echo \$$VAR
>
> - the new and shiny (V2+) ;-)
> # echo ${!VAR}
[...]

You forgot some quotes.

eval "printf '%s\n' \"\$$VAR\""
but best is to use a temporary variable:

eval "value=\$$VAR"
printf '%s\n' "$value"

${!VAR} is the ksh93 method that can also be found in some
versions of bash.

The zsh method is ${(P)VAR}

See also:

foo=bar
VAR='$foo'
printf '%s\n' ${(e)VAR}

Of course, none ${!VAR}, ${(P)VAR}, ${(e)VAR} are standard so
shouldn't be used in portable sh scripts.

--
Stéphane

Re: ${${var}}

am 07.11.2007 18:22:43 von Loki Harfagr

On Wed, 07 Nov 2007 16:41:16 +0000, Stephane CHAZELAS wrote:

> 2007-11-07, 15:56(+00), loki harfagr:
>> On Wed, 07 Nov 2007 15:26:36 +0000, Anoop wrote:
>>
>>> How can I get the value of the value of a variable. I tried using
>>> $"$var and of course $$var ($$ gives the pid) wouldnt work.
>>>
>>> I also tried $"${var}" - none worked and i get the error bad
>>> substitution. I could somewhat make it to work with eval and echo and
>>> `` (back quotes) and all. But I am sure there is a simpler way. Also I
>>> am using this as variables to generate a long path for find etc., that
>>> I would like to keep it smaller.
>>>
>>>
>> This is indirect referencing, you have two flavors:
>> - the old and smokey:-O
>> # eval echo \$$VAR
>>
>> - the new and shiny (V2+) ;-)
>> # echo ${!VAR}
> [...]
>
> You forgot some quotes.

Quite on purpose, it was more I didn't want to let the OP believe
he had to go into quotequoting hell (like the '"'"' family)
and the main topic of my post was to lead him to read the abs
pages (in which I believe there still are many lines written
by yourself ;-)


> eval "printf '%s\n' \"\$$VAR\""
> but best is to use a temporary variable:
>
> eval "value=\$$VAR"
> printf '%s\n' "$value"

Agreed, and I'd daresay that whenever a script needs
heavy magic it's probably time to use more adapted languages
(to be chosen according to the aim) or to start writing
well structured *and* commented scripts ;D)

>
> ${!VAR} is the ksh93 method that can also be found in some versions of
> bash.

Yep, but I thought it was overall acquired in Bash V2?

> The zsh method is ${(P)VAR}
>
> See also:
>
> foo=bar
> VAR='$foo'
> printf '%s\n' ${(e)VAR}
>
> Of course, none ${!VAR}, ${(P)VAR}, ${(e)VAR} are standard so shouldn't
> be used in portable sh scripts.

The existence of a "portable sh script" always reminds me of
the story of "Nessie"; I'm waiting for two "old little girls" to
confess it was a joke ;D)

Re: ${${var}}

am 07.11.2007 19:16:58 von cfajohnson

On 2007-11-07, loki harfagr wrote:
> On Wed, 07 Nov 2007 16:41:16 +0000, Stephane CHAZELAS wrote:
....
>> ${!VAR} is the ksh93 method that can also be found in some versions of
>> bash.
>
> Yep, but I thought it was overall acquired in Bash V2?

It was introduced in bash2, and has been included ever since.

It has never worked for me in ksh93:

$ b=a
$ a=1
$ echo ${!b}
b


--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: ${${var}}

am 07.11.2007 19:53:02 von Stephane CHAZELAS

2007-11-7, 13:16(-05), Chris F.A. Johnson:
> On 2007-11-07, loki harfagr wrote:
>> On Wed, 07 Nov 2007 16:41:16 +0000, Stephane CHAZELAS wrote:
> ...
>>> ${!VAR} is the ksh93 method that can also be found in some versions of
>>> bash.
>>
>> Yep, but I thought it was overall acquired in Bash V2?
>
> It was introduced in bash2, and has been included ever since.
>
> It has never worked for me in ksh93:
>
> $ b=a
> $ a=1
> $ echo ${!b}
> b
[...]

Oops, yes. ${!b} means something else in ksh93 indeed, I stand
corrected.

It's actualy quite the opposite.

In ksh93,

VAR=foo
foo=bar

If you do typeset -n VAR, VAR becomes a nameref.

So that when you read/write VAR, you actually access foo.

So printf '%s\n' "$VAR" gives actually "bar".

${!VAR}, then is a way to obtain the actually value of $VAR:

printf '%s\n' "${!VAR}" will actually print "foo".

I wonder if the bash authors actually misinterpreted the ksh
manual when they introduced their ${!VAR}.

A similar feature in zsh is the tying of an array to a scalar in
the fashion of $path wrt to $PATH.

typeset -T LD_LIBRARY_PATH ld_library_path :
ties the ld_library_path array to the LD_LIBRARY_PATH variable,
so that for instance ld_library_path+=(~/lib) adds ~/lib to the
$LD_LIBRARY_PATH variable.

Note that given that ${!} is actually a valid variable, the
${!var} was a poor choice of syntax. For instance, is ${!#}
expanding to the variable referenced by $# (or in bash the the
last positional argument) or to the content of $! with nothing
removed from its beginning (as in ${var#pattern}), Same for
${!-}, ${!?}.

--
Stéphane

Re: ${${var}}

am 07.11.2007 20:07:14 von Janis Papanagnou

Chris F.A. Johnson wrote:
> On 2007-11-07, loki harfagr wrote:
>
>>On Wed, 07 Nov 2007 16:41:16 +0000, Stephane CHAZELAS wrote:
>
> ...
>
>>>${!VAR} is the ksh93 method that can also be found in some versions of
>>>bash.
>>
>> Yep, but I thought it was overall acquired in Bash V2?
>
>
> It was introduced in bash2, and has been included ever since.
>
> It has never worked for me in ksh93:

Because in ksh93 that syntax is defined for use with namerefs to
get the name of the referenced variable...

$ nameref b=a
$ a=1 # irrelevant
$ echo ${!b}
a

(I don't like if the most popular shells use the same syntax for
different purposes.)

Janis

>
> $ b=a
> $ a=1
> $ echo ${!b}
> b
>
>

Re: ${${var}}

am 08.11.2007 03:37:53 von brian_hiles

Stephane CHAZELAS wrote:
> Chris F.A. Johnson wrote:
> > ...
> Note that given that ${!} is actually a valid variable, the
> ${!var} was a poor choice of syntax. For instance, is ${!#}
> expanding to the variable referenced by $# (or in bash the the
> last positional argument) or to the content of $! with nothing
> removed from its beginning (as in ${var#pattern}), Same for
> ${!-}, ${!?}.

Is somebody talking about syntactic ambiguity? ;) ;)

DGK used to be so careful to design new syntax, but now
there is still more ambiguous usage in the new features
implemented in ksh93 version "r" an newer:

<# ((expr)) : seek to expr byte offset for fd n.
># ((expr)) : seek to expr byte offset for fd n.
("SEEK_HOLE", "EOF", and "HOLE" are substitutable
variables in the arithmetric expression)

Since "<" and ">" are operators (constructed from meta-
characters), not keywords, the "#" should still be construed
as a comment delimiter. Ambiguous! If he had just used
<((...)) and >((...)), this could have been disambiguated
from process substitution syntax in the same fashion that
the new command substitution syntax was disambiguated
from subshell grouping, via a simple caveat in the usual
documentation.

When I upgraded my Bourne shell parser to also work with
ksh88, I only had to add about ten lines to my script. Now,
though....

(BTW, do namerefs also apply to shell _parameters_, that
is, not just ANSI variables?)

=Brian

Re: ${${var}}

am 08.11.2007 04:24:06 von Anoop kumar V

On Nov 7, 9:37 pm, bsh wrote:
> Stephane CHAZELAS wrote:
> > Chris F.A. Johnson wrote:
> > > ...
> > Note that given that ${!} is actually a valid variable, the
> > ${!var} was a poor choice of syntax. For instance, is ${!#}
> > expanding to the variable referenced by $# (or in bash the the
> > last positional argument) or to the content of $! with nothing
> > removed from its beginning (as in ${var#pattern}), Same for
> > ${!-}, ${!?}.
>
> Is somebody talking about syntactic ambiguity? ;) ;)
>
> DGK used to be so careful to design new syntax, but now
> there is still more ambiguous usage in the new features
> implemented in ksh93 version "r" an newer:
>
> <# ((expr)) : seek to expr byte offset for fd n.># ((expr)) : seek to expr byte offset for fd n.
>
> ("SEEK_HOLE", "EOF", and "HOLE" are substitutable
> variables in the arithmetric expression)
>
> Since "<" and ">" are operators (constructed from meta-
> characters), not keywords, the "#" should still be construed
> as a comment delimiter. Ambiguous! If he had just used
> <((...)) and >((...)), this could have been disambiguated
> from process substitution syntax in the same fashion that
> the new command substitution syntax was disambiguated
> from subshell grouping, via a simple caveat in the usual
> documentation.
>
> When I upgraded my Bourne shell parser to also work with
> ksh88, I only had to add about ten lines to my script. Now,
> though....
>
> (BTW, do namerefs also apply to shell _parameters_, that
> is, not just ANSI variables?)
>
> =Brian

I didnt know about the ${!var} - that works great in bash as well as
ksh. But my requirement is slightly more than what I originally asked
- I need to get the value of a variable which is the result of the
concatenation of the value obtained to another string. I have an
example here of what I intend to achieve eventually..

## I intend to get the SUCCESS string using abc and gh only. I can get
the value def from $abc. I could only achieve it using eval - is there
a better way probably using ${!var} or something? I am trying to avoid
eval (~ is my unix prompt and i am using ksh in my scripts)

~ export abc=def
~ export defgh=SUCCESS
~ echo ${!abc}gh
gh
~ echo ${abc}gh
defgh
~ echo $!{abc}gh
-bash: !{abc}gh: event not found
~ echo $"!{abc}gh"
-bash: !{abc}gh: event not found
~ echo \$${abc}gh
$defgh
~ eval echo \$${abc}gh
SUCCESS


Thank you so much all for the wonderful options.

Anoop

Re: ${${var}}

am 08.11.2007 09:24:30 von Stephane CHAZELAS

2007-11-08, 03:24(-00), Anoop:
[...]
> ## I intend to get the SUCCESS string using abc and gh only. I can get
> the value def from $abc. I could only achieve it using eval - is there
> a better way probably using ${!var} or something? I am trying to avoid
> eval (~ is my unix prompt and i am using ksh in my scripts)
>
> ~ export abc=def
> ~ export defgh=SUCCESS
> ~ echo ${!abc}gh
> gh
> ~ echo ${abc}gh
> defgh
> ~ echo $!{abc}gh
> -bash: !{abc}gh: event not found
> ~ echo $"!{abc}gh"
> -bash: !{abc}gh: event not found
> ~ echo \$${abc}gh
> $defgh
> ~ eval echo \$${abc}gh
> SUCCESS
[...]

eval "value=\${${abc}gh}"

${!var} is not better, because it's generally not better to write bash
scripts when you can write POSIX sh scripts instead.

In ksh93:

nameref val="${abc}gh"
print -r -- "$val"

In zsh:

val="\${${abc}gh}"
print -r -- ${(e)val}

Or:

print -r -- ${(P)${:-${abc}gh}}
or
print -r -- ${(e):-\${${abc}gh}}

--
Stéphane