01. For loops for, in, do, done

Loops are used to execute the a statement of tasks a repeated number of times or until a condition is met. There are two types of loops used in shell scripting - the for and while loops. Let's start with the for loop, which can take on a variety of tasks.


The keywords used in a for-loop are for, in, do, and done. Here is the structure for a for-loop.

for variableName in list
  some commands

1) Iterate through a list

One way to run through a for loop is to iterate through a list. This list may consist of strings or numbers.

for food in "bbq" "french fries" "coleslaw"
  echo "I like $food."
  | Here's the output:
$ ./test
I like bbq.
I like french fries.
I like coleslaw.

Remember that with brace expansions, you can list a range of numbers or letters.

printf "Press enter to launch rocket ship: "
for num in {5..1}
  echo "$num..."
  sleep 1
for rocket in 'Blastoff!' '    A    ' '   / \   ' '       | |=|   ' '   | |   ' '   | |   ' '  _|=|_  ' ' / | | \ '     | ' | \,/ | ' ' |/" "\| ' ' """"""" '
  echo "$rocket"
  sleep 1

Try copying and pasting this code snippet and running it to see a rocketship blast off!

2) Repeat per output of a command

You may also loop through outputs of a command.

for output in $(ls)
  printf "File %2d: %s\n" $count $output

This script simply takes the output of an ls command and lists them in order with an integer marking its position.

$ ./test
File  1: Applications
File  2: Desktop
File  3: Documents
File  4: Downloads
File  5: Dropbox
File  6: Library
File  7: Movies
File  8: Music
File  9: Pictures
File 10: Public

3) Three-step for loops

In most programming language, there are three-step for loops. The first expression is the variable declaration, the second is the loop condition test and the third is some evaluation that updates the expression.

To use such a three-step for loop in shell scripting, use the following syntax:

for (( i=1; i <= 5; i++ ))
  echo "$i Mississippi..."
  sleep 1
echo "Ready or not here I come!"

The result is simply:

$ ./test
1 Mississippi...
2 Mississippi...
3 Mississippi...
4 Mississippi...
5 Mississippi...
Ready or not here I come!

To get an inifinite loops, simply omit all three placeholders.

for (( ; ; ))
  echo "ctrl+c to stop this runaway infinite loop!"

4) Looping over command line arguments

If you remember from our previous tutorial, the $@ variable stores all command line arguments. We can thus use the $@ as a list and loop over our arguments.

However, placing the $@ is optional. By default, the in $@ is already included.

for i 
  case $i in

5) Looping specific files within directory

We can also use a for loop to iterate through items of a directory. Here, we take all text files within the current directory and stitch them together.

for file in ./*
  if [ ${file: -4} == ".txt" ]
    cat $file >> concatFile.txt 

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources