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 IF-statements in a Bash-script

Commands, Syntax, and Examples

By

With an if-statement, which is a type of conditional statement, you can perform different actions depending on specified conditions. It effectively gives the system the ability to make decisions.

An example of the simplest form of an if-statement would be:

count=5
if [ $count == 5 ]
then 
   echo "$count"
fi

In this example the variable "count" is used to specify a condition that is used as part of the if-statement. Before the if-statement is execute, the variable "count" is assigned the value "5". The if-statement then checks whether the value of "count" is "5". If that is the case the statement between the keywords "then" and "fi" are executed, otherwise any statements following the if-statement are executed. The keyword "fi" is "if" spelled backwards. The bash scripting language uses this convention to mark the end of a complex expression, such an if-statement or case-statements.

The "echo" statement prints its argument, in this case the value of the variable "count", to the terminal window. Indentation of the code between the keywords of the if-statement improves readability, but is not necessary.

If you have a situation where a piece of code should be executed only if a condition is not true, you can use the keyword "else" in a if-statement, as in this example:

count=5
if [ $count == 5 ]
then 
   echo "$count"
else
   echo "count is not 5"
fi

If the condition "$count == 5" is true, the system prints the value of the variable "count", otherwise it prints the string "count is not 5".

If you want to differentiate between multiple conditions, you can use the keyword "elif", which is derived from "else if", as in this example:

if [ $count == 5 ]
then 
   echo "count is five"
elif [ $count == 6 ]
then
   echo "count is six"
else
   echo "none of the above"
fi

If "count" is "5", the system prints out "count is five". If "count" is not "5" but "6", the system prints "count is six". If it is neither "5" nor "6", the system prints "none of the above".

As you may have guessed, you can have any number of "elif" clauses. An example with multiple "elif" conditions would be:

if [ $count == 5 ]
then 
   echo "count is five"
elif [ $count == 6 ]
then
   echo "count is six"
elif [ $count == 7 ]
then
   echo "count is seven"
elif [ $count == 8 ]
then
   echo "count is eight"
elif [ $count == 9 ]
then
   echo "count is nine"
else
   echo "none of the above"
fi

A more compact way to write such statements with multiple conditions is the case method. It functions similar to the if-statement with multiple "elif" clauses, but is more concise. For example, the above piece of code can be re-written with the "case" statement as follows:

case "$count" in
     5) 
        echo "count is five"
        ;;
     6) 
        echo "count is six"
        ;;
     7) 
        echo "count is seven"
        ;;
     8) 
        echo "count is eight"
        ;;
     9) 
        echo "count is nine"
        ;;
     *)
        echo "none of the above"
esac

If-statements are often used inside for-loops or while-loops as in this example:

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

You can also have nested if statements. Simplest nested if statement is of the form: if...then...else...if...then...fi...fi. However, if-statement can nested with arbitrary complexity.

See also the section on How to Pass Arguments to a Bash Script, which shows how to use conditionals to process parameters passed from the command line.

The Bash shell provides other programming constructs, such as for-loops, while-loops, and arithmetic expressions.

  1. About.com
  2. Technology
  3. Linux
  4. Linux HowTos
  5. Bash How-To's
  6. Bash If Statement (Conditionals)

©2014 About.com. All rights reserved.