You are not logged in.

#1 2008-11-27 02:59:34

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Bash: Variable with variable name

I have a variable with a variable name in it. I think I once saw an easy way to get the value of the second variable, but all that seems to work for me is with eval:

$ var=foo
$ foo=bar
$ eval echo \$$var
bar

How can this be done without eval?

Offline

#2 2008-11-27 12:11:56

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Bash: Variable with variable name

that's what's used in /etc/rc.d/network

Offline

#3 2008-11-27 12:37:48

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: Bash: Variable with variable name

echo ${!var}

This is called indirection. See http://wooledge.org:8000/BashFAQ/006


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#4 2008-11-27 12:54:41

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash: Variable with variable name

Thanks, works great.

Offline

#5 2008-11-27 13:03:09

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,384
Website

Re: Bash: Variable with variable name

Dieter@be wrote:

echo ${!var}

This is called indirection. See http://wooledge.org:8000/BashFAQ/006

Cool.  Bash knowledge++.

Offline

#6 2008-11-27 13:36:35

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Bash: Variable with variable name

awesome

Offline

Board footer

Powered by FluxBB