Search code examples
shellvariablessetkshtypeset

KSH Shell Double Variable names?


I am trying to create a variable based on $i

i=1
line="one two three four five six"

while [[ $i -lt 3 ]]; do 
set string$i=`echo $line | cut -d" " -f1-3`
echo $string$i
do_stuff_here
done

when i do this I get the following output

1

the expected output is

one two three

In Fact when I echo $String1...i get the expected output...so its stored correctly. I know its the way I am calling $string$1...but I have tried all kinds of quotes/parenthesis and Its not working. Can someone tell me how to call my variable?


Solution

  • Your while loop never stops unless you increment i in do_stuff_there, anyway, this should be closer to what you are expecting:

    i=1
    line="one two three four five six"
    
    while [[ $i -lt 3 ]]; do
      eval string$i=\"`echo $line | cut -d" " -f1-3`\"
      eval echo \$string$i
      do_stuff_here
    done