LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   generate dynamic variable in unix (https://www.linuxquestions.org/questions/linux-general-1/generate-dynamic-variable-in-unix-942933/)

vaibhavs17 05-02-2012 09:59 AM

generate dynamic variable in unix
 
S1= 23450
J=1

I would like to get print value as below

echo $SJ
23450

s2= 23490

J=2

echo $S2
23490

but how can I achive in unix shell using echo statement, I am using such logic in loop and J is the counter.

catkin 05-02-2012 11:00 AM

What you are asking for can be done in bash but is messy and potentially dangerous. An array solution would be better:
Code:

S[1]=23450
J=1
echo ${S[J]}


pan64 05-02-2012 11:06 AM

I think you need the eval
try
eval echo \$S$J

(and be careful s2 and S2 are not the same)


All times are GMT -5. The time now is 10:56 PM.