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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 15 comments… read them below or add one }
for i in {1..10}
does not work in KSH.
You must have tried this with BASH or compagnion.
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
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.
I’ve tested this on KSH-93. Can you find out your ksh version?
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.
Hey,
It doesn’t work on tcsh shell. whats the way to iterate for loop for 1 to 100.
Thanks
Ruchi
same problem, try
for ((i=0;i<100;i++))
do
echo $i
done
Try
#!/usr/bin/ksh
n=1
while [[ $n -le 100 ]]; do
echo $n
((n+=1))
done
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.
can any one say -In which shell ,script can be written in Aix?.
For iterating sequence of numbers you can use ‘while’ like posted in this thread:
ex:
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 .
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}
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