Prev: sending characters to the standard input while a program is running
Next: *** Project for New American Unix, Physics, Electronics, Metalworking - TUTORIAL VIDEOS ***
From: James on 4 Aug 2010 14:01 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 4 Aug 2010 17:39 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 4 Aug 2010 20:10
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) |