|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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) |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|