Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Is there a way to use a variable in a veriable name?

SOLVED
Go to Solution
Honored Contributor

Is there a way to use a variable in a veriable name?

If I have the following [using ksh]

num_1_num=abc
num_2_num=def
num_3_num=ghi

x=2
Is there some way to do the following?
echo $num_($x)_num

I don't want to use an array because the initial variables are already set

1 ACCEPTED SOLUTION
Honored Contributor Honored Contributor

Re: Is there a way to use a variable in a veriable name?

n_1_a=abc
n_2_a=def
n_3_a=ghj

x=2
a=n_${x}_a
eval echo \$$a
3 REPLIES
Honored Contributor Honored Contributor

Re: Is there a way to use a variable in a veriable name?

n_1_a=abc
n_2_a=def
n_3_a=ghj

x=2
a=n_${x}_a
eval echo \$$a
Honored Contributor Honored Contributor

Re: Is there a way to use a variable in a veriable name?

or:
"fsm" 7 lines, 72 characters
n_1_a=abc
n_2_a=def
n_3_a=ghj

x=2
eval echo \${n_${x}_a}

Highlighted
Honored Contributor

Re: Is there a way to use a variable in a veriable name?

Thank you oldschool.

That's what I'll do.