Use of "eval" in creating new variable names

paul.d.finkel pfinkel at cbnewsb.cb.att.com
Wed May 15 01:48:54 AEST 1991


In article <12291 at mentor.cc.purdue.edu> asg at sage.cc.purdue.edu (The Grand Master) writes:
>In article <1991May13.180547.21281 at cbfsb.att.com> pfinkel at cbnewsb.cb.att.com (paul.d.finkel) writes:
>}I have tried things like: eval tty_numb"$count"=`echo $2`
>}If I then try to echo the contents of my new variable, all I get is the number:
>}Example: echo $tty_numb"$count"    This will give me    1
>}expect it. Example: echo $tty_numb1   This will give me  tty17
>}echo $tty_numb"$numb" #This where the user chooses a number associated with
>
> Try:
>eval echo $tty_numb$count
>---------


	It took me a while, but here it is:

	eval tty_numb"$count"=`echo $2`

	# This line will create a variable named "tty_numb1".
	# Its contents will be (for example) "tty12"

	# Later on I will prompt user for the number corresponding to the user

	echo "Enter number: \c"
	read number

	#Then I can rereference (if such a word exists) my variable like this:

	eval echo $"tty_numb$number"

	#                   ^^^^^^^ will be evaluated first leaving
	#    echo $tty_numb1
	#
	#    Then ^ will be evaluated
	#    and eventually my correct tty number will be echoed.
	#    I also created a variable for the user's name called:
	#    nuname"$count". It is rereferenced in the same fashion.

	#Thank you for your help!



More information about the Comp.unix.shell mailing list