≡ Menu

KSH For Loop Examples

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
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 16 comments… add one }

  • Kai March 13, 2009, 10:32 am

    for i in {1..10}

    does not work in KSH.

    You must have tried this with BASH or compagnion.

  • Wilfrid May 14, 2009, 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

  • H Mark July 7, 2009, 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.

  • nixCraft July 7, 2009, 5:16 pm

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

    ksh --version
  • Peter Charpentier July 21, 2009, 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.

  • ruchi August 31, 2009, 10:50 am

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

  • tom August 31, 2009, 11:42 am

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

  • nixCraft August 31, 2009, 11:55 am

    Try

    #!/bin/csh
    foreach i ( 1 2 3 )
    	echo "welcome $i times"
    end
  • Billy Baroo October 14, 2009, 7:37 pm

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

  • Carlos Ijalba January 20, 2010, 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.

  • RaMa March 29, 2010, 11:19 am

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

  • Cyro August 7, 2010, 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
    
    • Coco123 May 30, 2013, 8:29 am

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

  • navneet June 16, 2011, 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 .

  • Dima March 5, 2012, 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}

  • Amin April 12, 2012, 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

Leave a Comment