7gdwga
Last Updated: February 25, 2016
·
842
· 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!

Say Thanks
Respond
Filed Under

Awesome Job

E20ce6a2 ca1f 11e7 82be 7f617c8310c9
SENIOR DEVELOPER (m/w)
·
Berlin, Köln, Hamburg, München
·
Full Time