For Loops

KSH for loop constructs

KSH for with list iteration

KSH for loop with list iteration

#!/bin/ksh
for item in apple banana cherry; do
    print "Fruit: $item"
done
KSH for with array iteration

KSH for loop iterating through array

#!/bin/ksh
set -A fruits apple banana cherry
for fruit in "${fruits[@]}"; do
    print "Fruit: $fruit"
done
KSH for with range

KSH for loop with numeric range

#!/bin/ksh
for i in {1..10}; do
    print "Number: $i"
done
KSH for with step

KSH for loop with step size

#!/bin/ksh
for i in {1..20..2}; do
    print "Odd number: $i"
done
KSH for with file globbing

KSH for loop with file globbing

#!/bin/ksh
for file in *.txt; do
    if [[ -f $file ]]; then
        print "Processing file: $file"
        wc -l "$file"
    fi
done
KSH for with command substitution

KSH for loop with command substitution

#!/bin/ksh
for user in $(cut -d: -f1 /etc/passwd); do
    print "User: $user"
done
KSH for with C-style syntax

KSH for loop using C-style syntax with arithmetic evaluation

#!/bin/ksh
for (( i=1; i<=10; i++ )); do
    print "Number: $i"
done
KSH for with step size

KSH for loop with custom step size using C-style syntax

#!/bin/ksh
for (( i=0; i<=20; i+=2 )); do
    print "Even number: $i"
done