PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > ${${var}}
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

${${var}}

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 15h26   #1
Anoop
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ${${var}}

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.

  Réponse avec citation
Vieux 07/11/2007, 15h46   #2
Tiago Peczenyj
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

On Nov 7, 1:26 pm, Anoop <anoopkum...@gmail.com> 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

  Réponse avec citation
Vieux 07/11/2007, 15h55   #3
Lew Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

On Nov 7, 10:26 am, Anoop <anoopkum...@gmail.com> 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


  Réponse avec citation
Vieux 07/11/2007, 15h56   #4
loki harfagr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

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
  Réponse avec citation
Vieux 07/11/2007, 16h41   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

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
  Réponse avec citation
Vieux 07/11/2007, 17h22   #6
loki harfagr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

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)

  Réponse avec citation
Vieux 07/11/2007, 18h16   #7
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

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 <http://cfaj.freeshell.org/shell/>
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
  Réponse avec citation
Vieux 07/11/2007, 18h53   #8
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ${${var}}

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14361 seconds with 16 queries