Last Updated: February 25, 2016
·
1.661K
· paolodt

Loop over ranges in BASH

The tricks in BASH are infinite. You may know that bash support loops thanks to the for statement:

for x in item1 item2 item3; do
  echo $x
done 

or if you prefer to be concise:

for x in item1 item2 item3; do echo $x; done

Not surprisingly, the above fragments will output:

item1
item2
item3

But what about if you need to iterate a range of numbers, let's say from 1 to 100? The for statement can iterate over a range using the syntax shown below:

for x in {1..100}; do echo $x; done

It supports even range of strings. For example:

for x in {A..Z}; do echo $x; done

Enjoy it!