KSH For Loop Examples

Posted on in Categories , , , , , , , , , , , , last updated August 31, 2009

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

20 comment

  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. 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
    
  6. 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 .

  7. 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}

  8. 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

  9. What if you have 5 or 6 files, specific files .biz in a directory and you want to store them one at the time in a variable $mirage, and want to process $mirage until no more .biz file are found.
    How will you modify the script accordingly

  10. #!/bin/ksh
    for i in {1..10} …………
    doesnt work with ksh. but still working with bash, Thanks.

  11. for i in ‘ls’ I could not get single one file or directory when using $i, instead of
    1.txt
    2.txt

Comments are closed.