Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

${$var} in bash

40 views
Skip to first unread message

James

unread,
May 11, 2016, 7:06:32 PM5/11/16
to
x=hello
y=world

for f in x y; do
echo $f=${$f}
done

How to do this correctly?

TIA
James

Barry Margolin

unread,
May 11, 2016, 7:35:39 PM5/11/16
to
In article <cc515fc0-3936-4bb0...@googlegroups.com>,
James <hsle...@yahoo.com> wrote:

> x=hello
> y=world
>
> for f in x y; do
> echo $f=${$f}
> done
>
> How to do this correctly?

echo $f=${!f}

From
http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansi
on.html#Shell-Parameter-Expansion

If the first character of parameter is an exclamation point (!), it
introduces a level of variable indirection. Bash uses the value of the
variable formed from the rest of parameter as the name of the variable;
this variable is then expanded and that value is used in the rest of the
substitution, rather than the value of parameter itself.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

James

unread,
May 11, 2016, 7:53:39 PM5/11/16
to
> echo $f=${!f}
>
> From
> http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansi
> on.html#Shell-Parameter-Expansion
>
> If the first character of parameter is an exclamation point (!), it
> introduces a level of variable indirection. Bash uses the value of the
> variable formed from the rest of parameter as the name of the variable;
> this variable is then expanded and that value is used in the rest of the
> substitution, rather than the value of parameter itself.
>
> --

Forgot about it. Got to read that link. Thanks.

James

Janis Papanagnou

unread,
May 11, 2016, 7:57:38 PM5/11/16
to
On 12.05.2016 01:06, James wrote:
> x=hello
> y=world
>
> for f in x y; do
> echo $f=${$f}
> done
>
> How to do this correctly?

While you can use bash specific constructs (as posted elsethread)
it's also not difficult with standard shell constructs...

eval echo $f=\${$f}


Janis

>
> TIA
> James
>

Chris F.A. Johnson

unread,
May 11, 2016, 8:08:05 PM5/11/16
to
On 2016-05-11, James wrote:
> x=hello
> y=world
>
> for f in x y; do
> echo $f=${$f}
> done
>
> How to do this correctly?

for f in x y; do
echo "$f=${!f}"
done

--
Chris F.A. Johnson
0 new messages