How to iterate Bash For Loop variable range Under Unix / Linux

https:‮www//‬.lautturi.com
How to iterate Bash For Loop variable range Under Unix / Linux

To iterate a Bash for loop over a range of values, you can use the seq command to generate the range of values and pass it to the for loop as a list.

Here is the basic syntax for using the seq command to generate a range of values and pass it to a Bash for loop:

for i in $(seq start end); do
    # commands
done

The seq command generates a sequence of numbers from start to end. The $(seq start end) command substitution generates a list of numbers from start to end and passes it to the for loop as the list of values to iterate over. The for loop will iterate over the list of values, and the variable i will be set to each value in turn.

For example, to iterate a for loop over the range of values from 1 to 10, you can use the following command:

for i in $(seq 1 10); do
    echo $i
done

This will print the numbers 1 through 10 to the terminal.

You can also specify a step size using the seq command.

For example, to iterate a for loop over the range of even numbers from 2 to 10, you can use the following command:

for i in $(seq 2 2 10); do
    echo $i
done

This will print the numbers 2, 4, 6, 8, and 10 to the terminal.

Created Time:2017-10-28 21:39:05  Author:lautturi