From: James on
What is the correct way in bash or zsh?

func() {
a=$1
echo -n "Enter $a: "; read $a
echo "$a is " ${$a}
}
func A


Enter A: "hello world"
${$a}: bad substitution


TIA
JL
From: Icarus Sparry on
On Wed, 04 Aug 2010 11:01:45 -0700, James wrote:

> What is the correct way in bash or zsh?
>
> func() {
> a=$1
> echo -n "Enter $a: "; read $a
> echo "$a is " ${$a}
> }
> func A
>
>
> Enter A: "hello world"
> ${$a}: bad substitution
>
>
> TIA
> JL

bash has a special syntax (copied from ksh93) in recent versions, look
for "indirect expansion" in the manual page.

echo "${a} is" ${!a}

zsh has lots of things, e.g.

echo "${a} is" ${(P)a}

ksh93 also has name references, see "typeset -n"
From: Chris F.A. Johnson on
On 2010-08-04, James wrote:
> What is the correct way in bash or zsh?
>
> func() {
> a=$1
> echo -n "Enter $a: "; read $a
> echo "$a is " ${$a}

eval "printf '$a is %s\n' \"\$$a\""

> }
> func A
>
>
> Enter A: "hello world"
> ${$a}: bad substitution


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)