KSH For Loop Examples

by on April 13, 2008 · 16 comments· LAST UPDATED August 31, 2009

in , ,

Q. How do I use for loop in Korn Shell under UNIX / Linux / BSD / OS X operating systems?

A.The main advantage of ksh over the traditional Unix shell is in its use as a programming language. KSH support for loop.

KSH Scripting: for loop syntax

The syntax is as follows:

for {Variable} in {lists}
do
	echo ${Variable}
done

Here is sample shell script to print welcome message 5 times:

#!/bin/ksh
for i in 1 2 3 4 5
do
  echo "Welcome $i times"
done

Run script as follows:
$ chmod +x script.ksh
$ ./script.ksh

OR
$ ksh script.ksh
You can easily set ranges (1 to 10) as follows:

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

You can also use variables to define the item list. They will be checked ONLY ONCE, when you start the loop.

#!/bin/ksh
files="/etc/passwd /etc/group /etc/hosts"
for f in $files; do
	if [ ! -f $f ]
	then
 		echo "$f file missing!"
	fi
done

Please do NOT quote "$list", if you want the for command to use multiple items.
Another example using for explicit list:

#!/bin/ksh
for car in bmw ford toyota nissan
   do
   print "Value of car is: $car"
done

KSH For Loop Command Substitution

Create a text file called spaceshuttles.txt as follows:

columbia
endeavour
challenger
discovery
atlantis
enterprise
pathfinder

Now create a shell script called demo.ksh

#!/bin/ksh
for shuttle in $(cat  spaceshuttles.txt)
do
        print "Current Space Shuttle : $shuttle"
done

You can also print file names in /tmp directory:

#!/bin/ksh
for f in $(ls /tmp/*)
do
        print "Full file path  in /tmp dir : $f"
done
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 16 comments… read them below or add one }

1 Kai March 13, 2009 at 10:32 am

for i in {1..10}

does not work in KSH.

You must have tried this with BASH or compagnion.

Reply

2 Wilfrid May 14, 2009 at 9:23 am

I don’t understand why you say that does not work :

#!/bin/ksh

# Nettoyage du contenu du fichier et suppression des derniers PDFs cree
>listePdfs.txt
cd /opt/filenet/temp_batch/800000PDFs
rm -rf *.pdf

for i in {1..1000}
do
echo “$i”
cp 0.src $i.pdf
echo “$i $i.pdf” >> listePdfs.txt
done

This programme work

Reply

3 H Mark July 7, 2009 at 5:14 pm

Hi Tried this using ksh, [Unix : SunOS 5.10 ]
and the output of {1..10}is
ksh Output : Welcome {1..10} times
, however when I change ksh to bash it
executes as expected.

Reply

4 nixCraft July 7, 2009 at 5:16 pm

I’ve tested this on KSH-93. Can you find out your ksh version?

ksh --version

Reply

5 Peter Charpentier July 21, 2009 at 7:27 am

The reason is that KSH does not really exist in Linux. If you look, then ksh is a symlink to bash. In Linux if you want to use really ksh, you have to download pdksh = Public Domain KSH.

In Solaris however it is a different story. There ksh is one shell and bash another one.

Reply

6 ruchi August 31, 2009 at 10:50 am

Hey,
It doesn’t work on tcsh shell. whats the way to iterate for loop for 1 to 100.
Thanks
Ruchi

Reply

7 tom August 31, 2009 at 11:42 am

same problem, try
for ((i=0;i<100;i++))
do
echo $i
done

Reply

8 nixCraft August 31, 2009 at 11:55 am

Try

#!/bin/csh
foreach i ( 1 2 3 )
	echo "welcome $i times"
end

Reply

9 Billy Baroo October 14, 2009 at 7:37 pm

#!/usr/bin/ksh
n=1
while [[ $n -le 100 ]]; do
echo $n
((n+=1))
done

Reply

10 Carlos Ijalba January 20, 2010 at 5:59 pm

Hi,

Yep, in AIX ksh the operator {1..10} doesn´t work, as well as ksh –version.
They only work in Linux, because it’s really the bash shell.

Reply

11 RaMa March 29, 2010 at 11:19 am

can any one say -In which shell ,script can be written in Aix?.

Reply

12 Cyro August 7, 2010 at 11:55 pm

For iterating sequence of numbers you can use ‘while’ like posted in this thread:
ex:

i=5 ; while [[ $i -lt 23 ]] ; do echo $i ; ((i += 1)) ; done

Reply

13 Coco123 May 30, 2013 at 8:29 am

Thanks a lot for this answer, work perfectly on AIX !

Reply

14 navneet June 16, 2011 at 2:55 pm

i am learning TCSH shell Loops and i needs a examples of foreach loops so kindly any one give me some examples of foreach loops .

Reply

15 Dima March 5, 2012 at 4:29 pm

Hi Vivek,

Your for each example:
#!/bin/ksh
for i in {1..10}
do
echo “Welcome $i times”
done

misses ; after for i in {1..10}

Reply

16 Amin April 12, 2012 at 4:23 am

Hi,
This is so cool:
end=20
step=3
i=0
while [ $((i+=$step)) -le $end ]; do
echo $i
done

output:
3
6
9
12
15
18

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: