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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining Shell Variables 1

Status
Not open for further replies.

blondem

Technical User
Nov 5, 2004
16
CA
Hello,

I'm writing a script in the Posix shell and I was wondering if it was possible to combine shell variables together as the shell variable name. For example

COUNT=1
CLIENT$COUNT=Bob --> note: the syntax doesn't work
echo $CLIENT1=Bob

Unfortunately the syntax in this example doesn't work.

I'm sure this can be done, but I haven't scripted anything in a while and would appreciate any help.

Cheers,
Mark
 
This works under ksh on AIX 5.1
Code:
g03201$ export COUNT=1
g03201$ export CLIENT${COUNT}=bob
g03201$ echo $CLIENT1
bob

Columb Healy
 
It also works in the POSIX shell on HP-UX 11i. Thanks for the help!
 
Hi:

eval should also work:

COUNT=1
eval CLIENT${COUNT}=bob
echo $CLIENT1
 
Use [tt]eval[/tt] !
The command [tt]export[/tt] has an (unwanted) side effect: it exports the variables.

Besides, [tt]eval[/tt] also allows you to get/use the values using constructed variable names:
Code:
eval echo \$CLIENT${COUNT}

--------------------

Denis
 
dchoulette: after use the [tt]export[/tt]'ed variable, you can [tt]unset[/tt] it.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top