Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #11217 > unrolled thread

need bash indirect variable help

Started byTodd <Todd@invalid.invalid>
First post2014-06-07 11:49 -0700
Last post2014-06-09 20:44 +0000
Articles 12 — 7 participants

Back to article view | Back to comp.os.linux.misc


Contents

  need bash indirect variable help Todd <Todd@invalid.invalid> - 2014-06-07 11:49 -0700
    Re: need bash indirect variable help William Unruh <unruh@invalid.ca> - 2014-06-07 23:42 +0000
      Re: need bash indirect variable help Eric Pozharski <whynot@pozharski.name> - 2014-06-08 12:19 +0300
        Re: need bash indirect variable help Eric Pozharski <whynot@pozharski.name> - 2014-06-09 15:07 +0300
    Re: need bash indirect variable help Richard Kettlewell <rjk@greenend.org.uk> - 2014-06-08 09:03 +0100
      Re: need bash indirect variable help Todd <Todd@invalid.invalid> - 2014-06-08 02:35 -0700
    Re: need bash indirect variable help Huibert Bol <huibert.bol@quicknet.nl> - 2014-06-08 10:29 +0200
      Re: need bash indirect variable help Todd <Todd@invalid.invalid> - 2014-06-08 02:36 -0700
    Re: need bash indirect variable help Jerry Peters <jerry@example.invalid> - 2014-06-08 20:31 +0000
      Re: need bash indirect variable help Todd <Todd@invalid.invalid> - 2014-06-08 14:53 -0700
        Re: need bash indirect variable help floyd@apaflo.com (Floyd L. Davidson) - 2014-06-08 14:50 -0800
          Re: need bash indirect variable help Jerry Peters <jerry@example.invalid> - 2014-06-09 20:44 +0000

#11217 — need bash indirect variable help

FromTodd <Todd@invalid.invalid>
Date2014-06-07 11:49 -0700
Subjectneed bash indirect variable help
Message-ID<lmvmr3$oqu$1@dont-email.me>
Hi All,

I am trying to access a variable based on a variable.

I have go this far:

x=abc
eval ${x}Status=xyz
echo $abcStatus
xyz

But, I won't know the exact name of the variable
($abcStatus).  How do I "echo" the variable indirectly?

Obviously

echo "${x}Status"

Doesn't work.

Many thanks,
-T

[toc] | [next] | [standalone]


#11219

FromWilliam Unruh <unruh@invalid.ca>
Date2014-06-07 23:42 +0000
Message-ID<ln081j$1nl$3@dont-email.me>
In reply to#11217
On 2014-06-07, Todd <Todd@invalid.invalid> wrote:
> Hi All,
>
> I am trying to access a variable based on a variable.
>
> I have go this far:
>
> x=abc
> eval ${x}Status=xyz
> echo $abcStatus
> xyz
>
> But, I won't know the exact name of the variable
> ($abcStatus).  How do I "echo" the variable indirectly?

The exact name is abcStatus. 
>
> Obviously
>
> echo "${x}Status"

Bash evaluates ${x} as abc and tacks it onto Status, so that this
command gives you anoutput of abcStatus. It does not give you the
contents of abcStatus. 
How you do a second layer substitution I do not know. Certainly 
echo ${${x}Status} 
does not work either. 

>
> Doesn't work.
>
> Many thanks,
> -T

[toc] | [prev] | [next] | [standalone]


#11222

FromEric Pozharski <whynot@pozharski.name>
Date2014-06-08 12:19 +0300
Message-ID<slrnlp8alv.f8o.whynot@orphan.zombinet>
In reply to#11219
with <ln081j$1nl$3@dont-email.me> William Unruh wrote:
> On 2014-06-07, Todd <Todd@invalid.invalid> wrote:

*SKIP*
>> Obviously
>> echo "${x}Status"
> Bash evaluates ${x} as abc and tacks it onto Status, so that this
> command gives you anoutput of abcStatus. It does not give you the
> contents of abcStatus.  How you do a second layer substitution I do
> not know. Certainly echo ${${x}Status} does not work either. 

Yeah, baby, I did it!

	{11:1} [0:0]% foo=bar
	{1663:2} [0:0]% eval ${foo}quux=baz
	{2096:17} [0:0]% echo $(eval $(echo echo \$${foo}quux)) 
	baz

Note: using backticks (either outside or inside) breaks to:

	{2110:18} [0:0]% echo `eval $(echo echo \$${foo}quux)`  
	14562{foo}quux

Second, it's zsh;  that might matter.

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

[toc] | [prev] | [next] | [standalone]


#11237

FromEric Pozharski <whynot@pozharski.name>
Date2014-06-09 15:07 +0300
Message-ID<slrnlpb8s7.9qq.whynot@orphan.zombinet>
In reply to#11222
with <slrnlp8alv.f8o.whynot@orphan.zombinet> Eric Pozharski wrote:

*SKIP*
> 	{2096:17} [0:0]% echo $(eval $(echo echo \$${foo}quux)) 
> 	baz

I've went terribly wrong route.  Might be a brain-fart.

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

[toc] | [prev] | [next] | [standalone]


#11220

FromRichard Kettlewell <rjk@greenend.org.uk>
Date2014-06-08 09:03 +0100
Message-ID<wwvegyzomjm.fsf@l1AntVDjLrnP7Td3DQJ8ynzIq3lJMueXf87AxnpFoA.invalid>
In reply to#11217
Todd <Todd@invalid.invalid> writes:
> I am trying to access a variable based on a variable.
>
> I have go this far:
>
> x=abc
> eval ${x}Status=xyz
> echo $abcStatus
> xyz
>
> But, I won't know the exact name of the variable
> ($abcStatus).  How do I "echo" the variable indirectly?
>
> Obviously
>
> echo "${x}Status"
>
> Doesn't work.

Use eval again.

$ eval echo \$${x}Status
xyz

-- 
http://www.greenend.org.uk/rjk/

[toc] | [prev] | [next] | [standalone]


#11223

FromTodd <Todd@invalid.invalid>
Date2014-06-08 02:35 -0700
Message-ID<ln1ank$6ng$1@dont-email.me>
In reply to#11220
On 06/08/2014 01:03 AM, Richard Kettlewell wrote:
> Todd <Todd@invalid.invalid> writes:
>> I am trying to access a variable based on a variable.
>>
>> I have go this far:
>>
>> x=abc
>> eval ${x}Status=xyz
>> echo $abcStatus
>> xyz
>>
>> But, I won't know the exact name of the variable
>> ($abcStatus).  How do I "echo" the variable indirectly?
>>
>> Obviously
>>
>> echo "${x}Status"
>>
>> Doesn't work.
>
> Use eval again.
>
> $ eval echo \$${x}Status
> xyz
>

Hi Richard,

     Perfect.  Thank you!

-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the riddle wrapped in an enigma wrapped
in a couple slices of baloney
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[toc] | [prev] | [next] | [standalone]


#11221

FromHuibert Bol <huibert.bol@quicknet.nl>
Date2014-06-08 10:29 +0200
Message-ID<ln16tm$i3a$1@dont-email.me>
In reply to#11217
Todd <Todd@invalid.invalid> wrote:

> Hi All,
>
> I am trying to access a variable based on a variable.
>
> I have go this far:
>
> x=abc
> eval ${x}Status=xyz
> echo $abcStatus
> xyz
>
> But, I won't know the exact name of the variable
> ($abcStatus).  How do I "echo" the variable indirectly?

Wouldn't an associative array be a better solution?

$ declare -A Status
$ x=abc
$ Status[$x]=xyz
$ echo ${Status[$x]}
xyz

-- 
Huibert
"Okay... really not something I needed to see."  --Raven

[toc] | [prev] | [next] | [standalone]


#11224

FromTodd <Todd@invalid.invalid>
Date2014-06-08 02:36 -0700
Message-ID<ln1apj$6ng$2@dont-email.me>
In reply to#11221
On 06/08/2014 01:29 AM, Huibert Bol wrote:
> Todd <Todd@invalid.invalid> wrote:
>
>> Hi All,
>>
>> I am trying to access a variable based on a variable.
>>
>> I have go this far:
>>
>> x=abc
>> eval ${x}Status=xyz
>> echo $abcStatus
>> xyz
>>
>> But, I won't know the exact name of the variable
>> ($abcStatus).  How do I "echo" the variable indirectly?
>
> Wouldn't an associative array be a better solution?
>
> $ declare -A Status
> $ x=abc
> $ Status[$x]=xyz
> $ echo ${Status[$x]}
> xyz
>

Hi Huibert,

    Not for the crazy scheme I came up with.  But
thank you anyway!

-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the riddle wrapped in an enigma wrapped
in a couple slices of baloney
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[toc] | [prev] | [next] | [standalone]


#11225

FromJerry Peters <jerry@example.invalid>
Date2014-06-08 20:31 +0000
Message-ID<ln2h7e$s1t$1@dont-email.me>
In reply to#11217
Todd <Todd@invalid.invalid> wrote:
> Hi All,
> 
> I am trying to access a variable based on a variable.
> 
> I have go this far:
> 
> x=abc
> eval ${x}Status=xyz
> echo $abcStatus
> xyz
> 
> But, I won't know the exact name of the variable
> ($abcStatus).  How do I "echo" the variable indirectly?
> 
> Obviously
> 
> echo "${x}Status"
> 
> Doesn't work.
> 
> Many thanks,
> -T

In bash:
~$ x=z
~$ a=x
~$ echo ${!a}
z

[toc] | [prev] | [next] | [standalone]


#11226

FromTodd <Todd@invalid.invalid>
Date2014-06-08 14:53 -0700
Message-ID<ln2m0f$1og$1@dont-email.me>
In reply to#11225
On 06/08/2014 01:31 PM, Jerry Peters wrote:
> Todd <Todd@invalid.invalid> wrote:
>> Hi All,
>>
>> I am trying to access a variable based on a variable.
>>
>> I have go this far:
>>
>> x=abc
>> eval ${x}Status=xyz
>> echo $abcStatus
>> xyz
>>
>> But, I won't know the exact name of the variable
>> ($abcStatus).  How do I "echo" the variable indirectly?
>>
>> Obviously
>>
>> echo "${x}Status"
>>
>> Doesn't work.
>>
>> Many thanks,
>> -T
>
> In bash:
> ~$ x=z
> ~$ a=x
> ~$ echo ${!a}
> z
>

Hi Jerry,

Thank you!

What exactly does the "!" do?

-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the riddle wrapped in an enigma wrapped
in a couple slices of baloney
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[toc] | [prev] | [next] | [standalone]


#11227

Fromfloyd@apaflo.com (Floyd L. Davidson)
Date2014-06-08 14:50 -0800
Message-ID<87mwdnm2wl.fld@barrow.com>
In reply to#11226
Todd <Todd@invalid.invalid> wrote:
>On 06/08/2014 01:31 PM, Jerry Peters wrote:
>>
>> In bash:
>> ~$ x=z
>> ~$ a=x
>> ~$ echo ${!a}
>> z
>>
>
>Hi Jerry,
>
>Thank you!
>
>What exactly does the "!" do?

See the bash man page, under the heading of "Parameter
Expansion", where the third paragraph down describes it.
Indirection, or they suggest "indirect expansion", uses
the contents of a variable as a the variable acted on.

It is the equivalent of the also suggested eval command,
which might be prefered just because it will be more
obvious what the eval command is doing while the bang
operator will be easily missed or confused.

-- 
Floyd L. Davidson                         http://www.apaflo.com/
Ukpeagvik (Barrow, Alaska)                      floyd@apaflo.com

[toc] | [prev] | [next] | [standalone]


#11239

FromJerry Peters <jerry@example.invalid>
Date2014-06-09 20:44 +0000
Message-ID<ln56ah$3u2$1@dont-email.me>
In reply to#11227
Floyd L. Davidson <floyd@apaflo.com> wrote:
> Todd <Todd@invalid.invalid> wrote:
>>On 06/08/2014 01:31 PM, Jerry Peters wrote:
>>>
>>> In bash:
>>> ~$ x=z
>>> ~$ a=x
>>> ~$ echo ${!a}
>>> z
>>>
>>
>>Hi Jerry,
>>
>>Thank you!
>>
>>What exactly does the "!" do?
> 
> See the bash man page, under the heading of "Parameter
> Expansion", where the third paragraph down describes it.
> Indirection, or they suggest "indirect expansion", uses
> the contents of a variable as a the variable acted on.
> 
> It is the equivalent of the also suggested eval command,
> which might be prefered just because it will be more
> obvious what the eval command is doing while the bang
> operator will be easily missed or confused.

The ${!x} notation has the advantage that you don't have to be as
careful as you do with eval, ie it's harder to sneak in an arbitrary
command if you're in some way processing user input.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.os.linux.misc


csiph-web