How to use IF ELSE statements in Shell Scripts

Overview

In this tutorial, you will learn how to use Bash IF Else statements in your shell commands.

The IF logical operator is commonly used in programming languages to control flow. You use it to check the state of a value, whether it is equal to another or not; or whether it is set or not, for example.

We’ll first look at the basic if statement is Bash, and then move onto if..else and if..elif..else.

IF Bash Statement

The basic syntax of an IF statement in bash looks like the following. If you have programming experience, you’ll notice it’s fairly different from what you are use to.

if TEST-COMMAND
then
     STATEMENTS
fi

The TEST-COMMAND is where you write your test case to check if something is true or not.

  • If your tests result is true, then a statement or number of statements will
  • If your test result is false, then no statement defined in your IF statement will be executed.

IF..Else Bash Statement

When writing an IF statement execute statements whether the test results true or false, you use the else operator.

if TEST-COMMAND
then
     STATEMENTS
else
     STATEMENTS
fi

By using the else operator, your if statement will execute a different set of statements depending on your test case.

  • If a test results in true, execute statements after then
  • If a test results in false, execute statements after the else logical operator.

IF ELIF Bash Statement

Multiple IF tests can be used within a single IF statement block. To do this with BASH or Shell Script the elif operator is used.

if TEST-COMMAND
then
     STATEMENTS
elif TEST-COMMAND
     STATEMENTS
else
     STATEMENTS
fi

In the example above, we’ve combined an if statement with an elif and and an else.

  • If the first test is true, execute statements immediately following.
  • If the first test is false and the second test case is true, execute statements immediately after the elif logical.
  • If the first two test cases return false execute statements immediately following the else logical.

Logical Operators

When you are performing your comparison tests you use an operator. Operators are provided for integer comparisons, string comparisons, and file comparisons.

Logical Operators for Integer Comparison

OperatorDescriptionExample
-gtGreater than[[ $var -gt 10 ]]
-geGreater than or equal to[[ $var -ge 10 ]]
-ltLess than[[ $var -lt 10 ]]
-leLess than or equal to[[ $var -le 10 ]]
-eqEqual to[[ $var -eq 10 ]]
-neNot equal to[[ $var -ne 10 ]]

Logical Operators for String Comparison

OperatorDescriptionExample
==is equal to[[ $var1 == "example" ]]
!=is not equal to[[ $var1 != $var2 ]]
-zString is null[ -z $var ]
-nString is not null[ -n $var ]
<ASCII value less than[[ $var < 10 ]]
>ASCII value greater than[[ $var > 10 ]]

Logical Operators for Files

OperatorDescriptionExample
-efile exists[[ -e /path/to/file ]]
-ffile is a regular file, /
not a directory
[[ -f /path/to/file ]]
-dFile is a directory[[ -d /path/to/directory ]]
-sFile is not zero size[[ -s /path/to/file ]]
-LFile is symbolic link[[ -L /path/to/file ]]
-bFile is a block device[[ -b /path/to/file ]]
-pFile is a pipe device[[ -p /path/to/file ]]
-SFile is a socket[[ -S /path/to/file ]]
-rFile has read permission for user.[[ -r /path/to/file ]]
-wFile has write permission for user.[[ -w /path/to/file ]]

Bash Integer Comparisons

If Greater Than or Else

To check if one value or variable is greater than a value you use the -gt flag in your test.

[[ x -gt y ]]

Used in an example, the following if logical checks whether the variable $foo is greater than 10.

if [[ $foo -gt 10 ]]
then
     echo $foo is greater than 10
else
     echo $foo is not greater then 10
fi 

If Less Than or Else

if [[ $foo -lt 10 ]]
then
     echo $foo is greater than 10
else
     echo $foo is not greater then 10
fi 

If Equal To or Else

if [[ $foo -eq 10 ]]
then
     echo $foo is greater than 10
else
     echo $foo is not greater then 10
fi 

Bash String Comparisons

If Variable is Null

To check if a variable is null (not set) use the -z string comparison operators. In the following example, we check to verify that the $foo variable is set.

if [[ -z $foo ]]
then
     echo "$foo is not set"
fi 

If Variable is not Null

To check if a variable is not null use the -n string comparison operator. For example, to check if the variable $bar is not null, you would write your if statement as follows.

if [[ -n $foo ]]
then
     echo "$foo is not set"
fi 

If Variable is Equal To String

To check whether two strings are equal or match each other, you use the == string comparison operator.

For example, to check whether $var1 is equal to "cats" you would write the following if statement.

if [[ $var1 == "cats" ]]
then
     echo "$var1 equals 'cats'"
fi