Coding, coding, coding....

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...

 

Comments

for i in {1..100}; do echo

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

is even more neat :-)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.