Linux – for loop range does not work ksh

for loop range does not work ksh… here is a solution to the problem.

for loop range does not work ksh

I tried,

#!/bin/ksh
for i in {1..10}
do
  echo "Welcome $i times"
done

In the KSH of the AIX machine.
The output I get is,

Welcome {1..10} times

What’s going on?
Isn’t it supposed to print from 1 to 10?
Edit:
According to Perkolator’s post, from Iterating through a range of ints in ksh?

It only works with Linux. Are there any other workarounds/alternatives for UNIX Box KSH?

for i in 1 2 3 4 5 6 7 8 9 10

It’s ugly.

Thank you.

Solution

Based on memory, I think the standard ksh on AIX is an older variant. It may not support range loops. Try running it with ksh93 instead of ksh. This should be in the same location as ksh, probably /usr/bin.

Otherwise, just use something old-fashioned, such as:

i=1
while [[ $i -le 10 ]] ; do
    echo "Welcome $i times"
    i=$(expr $i + 1)
done

In fact< look at the a href="http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=/com.ibm.aix.baseadmn/doc/baseadmndita/korn_shell_enhanced.htm" rel="noreferrer noopener nofollow" > Publib seems to confirm this (ksh93 snippet), so I’ll try to go down this route.

Related Problems and Solutions