Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create variable name that contains a variable 1

Status
Not open for further replies.

pavNell

Technical User
Sep 27, 2002
178
US
trying to create new variables whose names contain a variable. For instance, I've tried...


x=2
while test $x -ge 1
do
var_$x="Hello $x"
x=`expr $x - 1`
done

echo $var_2
echo $var_1


this script outputs...

var_2=Hello 2: not found
var_1=Hello 1: not found

thanks for any help.
 
I'm using the bash shell on my zaurus.

I'm not getting it. Guess it's the syntax for eval that I don't understand. I've read over the man page and tried several different ways to use eval. Help please???
 
Hi

Example. The [tt]bash[/tt] internal variables for the command prompts are [tt]PS1[/tt], [tt]PS2[/tt], [tt]PS3[/tt] and [tt]PS4[/tt]. You want to display their values in a loop, then assign them new values as (1)>, (2)>, (3)> and (4)> respectively.
Code:
for ((i=1;i<=4;i++)); do eval "echo PS$i = \$PS$i"; done

for ((i=1;i<=4;i++)); do eval "PS$i=\"($i)> \""; done

Feherke.
 
Thank you feherke, it worked!

My attempts were close. I didn't add or escape the extra double-quotes. Also the for loop looks cleaner than using a while loop.

A star for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top