[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays ,
The following code works

for ctcol in 2 3 4 5 6;
do 
set -A a$ctcol $(cut -d, -f $ctcol test_file)
done

how ever this does not work

for ctcol in {2..6};
do 
set -A a$ctcol $(cut -d, -f $ctcol test_file)
done

Kindly let me know what is wrong with it

From what I know, range expansion {2..6} is a part of modern bash shell's parameter expansion feature and not a feature of ksh.

An alternative that will work for sure (even in old bourne shells):

i=2
while [ $i -le 6 ]
do
    <statements>
    i=`expr $i + 1`
done

Another alternative that will work on ksh:

for i in $(seq 2 6)
do
    <statements>
done
1 Like

In any version of ksh newer than November 16, 1988, you can also use:

for ((i=2; i<=6; i++))
do  <statements>
done

Although not available in the original 1993 version of ksh, the:

for ctcol in {2..6}
do  ...
done

does work in recent versions of ksh93 including version s+ ( sh (AT&T Research) 1993-12-28 s+ ) as provided with Apple's OS X 10.7.5.

And, in any standards conforming shell (including ksh versions newer than 1988 and bash) you could also use:

i=2
while [ $i -le 6 ]
do  <statements>
    ((i++))
done

The seq utility suggested above by balajesuri will work on most Linux systems, but is not available on some UNIX systems.

2 Likes

Thanks for the detailed explanation along with the release dates of ksh. Very informative :b:

Thanks that was really helpful :slight_smile: