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

In Bash, you can use the for loop to iterate over a range of values. One way to specify the range is using the seq command, which generates a sequence of numbers.

Here’s an example of using seq in a for loop to iterate over a range of numbers from 1 to 10:

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

This will print each number in the range from 1 to 10 on a separate line.

Another way to specify the range is to use the {start..end} syntax. Here’s an example that does the same thing as the previous example:

for i in {1..10}; do
echo $i
done

You can also specify the increment value by using the {start..end..increment} syntax. For example, to print every other number in the range from 1 to 10, you could use the following:

for i in {1..10..2}; do
echo $i
done

This would print 1 3 5 7 9 on separate lines.

(https://rpdrlatino.com)

Leave a Comment