1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Discuss in my forum

How to write bash WHILE-loops

Commands, Syntax, and Examples

By

Businessman typing on computer keyboard
Dominik Pabis/E+/Getty Images
You can execute a sequence of commands by writing them into a "script file" and then running the script file. A script file is simply a text file that contains a sequence of instructions that could also be executed from the command line (also know as shell). Usually the extension ".sh" is used for script files.

Here is an example of a while loop:

#!/bin/bash
count=1
while [ $count -le 9 ]
do
    echo "$count"
    sleep 1
    (( count++ ))
done

When executed, this script file will print the numbers 1 through 9 on the screen. The while-statement gives you more flexibility for specifying the termination condition than the for-loop. For example you can make the previous script an infinite loop by omitting the increment statement "(( count++ ))":

#!/bin/bash
count=1
while [ $count -le 9 ]
do
    echo "$count"
    sleep 1
done

The "sleep 1" statement pauses the execution for 1 second on each iteration. Use "Ctrl-C" to terminate the process.

You can also create an infinite loop by putting a colon as the condition:

#!/bin/bash
count=1
while :
do
    echo "$count"
    sleep 1
    (( count++ ))
done

In order to use multiple conditions in the while-loop you need to use the double square bracket notation:

count=1
done=0
while [[ $count -le 9 ] && [ $done == 0 ]]
do
    echo "$count"
    sleep 1
    (( count++ ))
    if [ $count == 5 ]; then  $done=1
    fi
done

In this script the variable "done" is initialized to 0 and then set to 1 when count reaches 5. The loop condition states that the while loop will continue as long as "count" is less than nine and "done" is equal to zero. Therefore the loops exits when count equals 5.

The "&&" means logical "and" and "||" means logical "or".

An alternative notation for the conjunctions "and" and "or" in conditions is "-a" and "-o" with single square brackets. The above condition

[[ $count -le 9 ] && [ $done == 0 ]]

could be rewritten as

[ $count -le 9 ] -a [ $done == 0 ]

Reading a text file is typically done with a while loop. In the following example, the bash script reads the contends of a file "inventory.txt" line be line:

FILE=inventory.txt
exec 6

The first line assigns the input file name to the variable "FILE". The second line saves the "standard input" in the file descriptor "6" (it could be any value between 3 and 9). This is done so that "standard input" can be restored to file descriptor "0" at the end of the script (see statement "exec 0 In the 3rd line the input file is assigned to file descriptor "0", which is used for standard input. The "read" statement then reads a line from the file on each iteration and assigns it to the variable "line1".

In order to prematurely exit a while-loop you can use the break statement as in the following example:

count=1
done=0
while [ $count -le 9 ]
do
    echo "$count"
    sleep 1
    (( count++ ))
    if [ $count == 5 ]
    then 
        break
    fi
done
echo Finished

The break statement skips program execution to the end while loop and executes any statements following it. In this case the statement "echo Finished".

The continue statement on the other hand skips only the rest of the while loop statement of the current iteration and jumps directly to the next iteration:

count=1
done=0
while [ $count -le 9 ]
do
    sleep 1
    (( count++ ))
    if [ $count == 5 ]
    then 
        continue
    fi
    echo "$count"
done
echo Finished

In this case the "continue" statement is executed when the variable "count" reaches 5. This means the subsequent statement (echo "$count") is not executed on this iteration (when the value of "count" is 5).

See also: the Bash for-loop.

  1. About.com
  2. Technology
  3. Linux
  4. Linux HowTos
  5. Bash How-To's
  6. How to write bash WHILE-loops

©2014 About.com. All rights reserved.