KSH For Loop Examples

by Vivek Gite on April 13, 2008 · 12 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 12 comments… read them below or add one }

1 Kai March 13, 2009

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

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

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 Vivek Gite July 7, 2009

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

ksh --version

Reply

5 Peter Charpentier July 21, 2009

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

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

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

Reply

8 Vivek Gite August 31, 2009

Try

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

Reply

9 Billy Baroo October 14, 2009

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

Reply

10 Carlos Ijalba January 20, 2010

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

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

Reply

12 navneet June 16, 2011

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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 5 + 4 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: