Fork me on GitHub

Project Notes

#028 Bash Loops

All about Bash loops

Notes

Iteration is a findamental construct that is often needed for our scripting needs. Bash provides a number of facilities for loops, including the built-in commands for, while and until.

Using for

The for command can be used to iterate a set or list of items. See for_in_list.sh for an example or using a fixed list. But anything that can product a list can also be used (like cat a file).

for fruit in "apples" "oranges" "pears"
do
  echo "loop with fruit==${fruit}"
done

Or can use a ranges. See for_in_range.sh for an example…

for i in {1..9}
do
  echo "iteration ${i}"
done

while

A while loop will execute until the test condition is not true.

n1=0
n2=5
while [ $n1 -le $n2 ]
do
  echo $n1
  n1=$(($n1+1))
done

See while.sh for an example…

$ ./while.sh 3
loop with n1==0
loop with n1==1
loop with n1==2
loop with n1==3
finished now n1==4

until

The until command is the complement of while: the loop will execute until the test condition is true.

n1=0
n2=5
until [ $n1 -ge $n2 ]
do
  echo $n1
  n1=$(($n1+1))
done

See until.sh for an example…

$ ./until.sh 3
loop with n1==0
loop with n1==1
loop with n1==2
finished now n1==3

Infinite Loops

Just require a condition that always passes into the loop.

e.g. true with a while loop:

while true
do
  echo "Press [CTRL+C] to stop.."
  sleep 1
done

or with for:

for (( ; ; ))
do
  echo "Press [CTRL+C] to stop.."
  sleep 1
done

One-liners

Simple loops can usually be collapsed to one-liners that can be used on the command line. For example:

$ for i in {1..3}; do echo "iteration ${i}"; done
iteration 1
iteration 2
iteration 3

Or an infinite loop..

$ echo "infinite loop on a single line [ hit CTRL+C to stop]" ;  for (( ; ; )) ; do date ; sleep 1 ; done
infinite loop on a single line [ hit CTRL+C to stop]
Mon Oct  9 22:33:26 SGT 2017
Mon Oct  9 22:33:27 SGT 2017
Mon Oct  9 22:33:28 SGT 2017

Credits and References

About LCK#28 Bash

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader