KSH For Loop Examples

w‮al.ww‬utturi.com
KSH For Loop Examples

Here are some examples of for loops in KSH (Korn Shell):

Basic for loop

``` sh # Iterate from 1 to 10 for i in {1..10}; do echo $i done ```

For loop with step size

``` sh # Iterate from 1 to 10 with a step size of 2 for i in {1..10..2}; do echo $i done ```

For loop over a range of numbers

``` sh # Iterate from 1 to 10 for ((i=1; i<=10; i++)); do echo $i done ```

For loop over an array

``` sh # Declare an array my_array=(apple orange banana)
Iterate through the array values

for value in "${my_array[@]}"; do
echo "$value"
done

<h2>For loop over the output of a command</h2>
``` sh
# Iterate over the output of the ls command
for file in $(ls); do
  echo "$file"
done

For more information on using for loops in KSH, you can consult the KSH manual or online documentation.

Created Time:2017-10-29 22:08:49  Author:lautturi