Ever needed to do a Bash loop?

The simple bash loop to do something with all files in the directory is:

for f in * ; do echo $f ; done

which just displays all files in this case.

This is easy, what I usually need and often forget how to do it is to execute some action exact number of times. Something like simple for loop in C or Java. And there is an exact match for such loop in Bash too:

for (( i=1 ; i<=100 ; ++i )) ; do echo $i ; done 

I found another similar solution on the Internet today at spiralbound.net:

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

which might useful in some cases...

 

Trackback URL for this post:

http://artur.hefczyc.net/trackback/22