How to use if, else, then, elif statements in a BASH script

Home > Search > How-to
  by

Equals, Greater than, Less than

An if statement that includes then and else can be used to do something if a condition is either true or false. In this example, "Foo does equal 1" will display if foo equals 1, and "Foo does not equal 1" will display if foo does not equal 1.

#!/bin/bash
foo=1
if [ $foo -eq 1 ]
then
  echo "Foo does equal 1"
else
  echo "Foo does not equal 1"
fi

 

The following comparison operators can also be used.

  • -ne (not equal to)
  • -gt (greater than)
  • -lt (less than)
  • -ge (greater than or equal to)
  • -le (less than or equal to)
  • -z (is null)

 


File exists

In this example, "The file /home/test exists" is displayed if there is a file named test in the /home director. "The file /home/test does not exist" is displayed if the test file does not exist.

#!/bin/bash
if [ -e /home/test ]; 
then
  echo "The file /home/test exists"  
else
  echo "The file /home/test does not exist"
fi

 


File contains

Let's say file1.txt contains some text. The following if statement can be used to determine if file1.txt contains "Hello World".

#!/bin/bash
if grep -q "Hello World" /path/to/file1.txt; 
then
  echo "File1.txt contains Hello World"  
else
  echo "File1.txt does not contain Hello World"
fi

 

Here is another way to do the same.

#!/bin/bash
file1=$(cat /path/to/file1.txt)

if [[ $file1 == *Hello ]] 
then
  echo "File1.txt contains Hello World"  
else
  echo "File1.txt does not contain Hello World"
fi

 


Read user input

Adding the read command to an if statement prompts a user for input. In this example, when prompted "Y for yes, N for no", if the user types Y, "You answered yes" is displayed. If the user types N, "You answered no" is displayed. 

#!/bin/bash
read -p "Y for yes, N for no : " answer
if [ $answer == "Y" ]
then
  echo "You answered yes"
else
  echo "You answered no"
fi

 

In this scenario, ensure you include a single white space inside of the brackets.

 


Else if

The prior script has a problem. The user must type exactly Y or N. If the user types something other than Y or N, the script will not know what to do. elif (else if) can be added to account for this.

#!/bin/bash
read -p "Y for yes, N for no : " answer
if [ $answer == "Y" ]
then
  echo "You answered yes"
elif [ $answer == "N" ]
then
  echo "You answered no"
else
  echo "You must answer Y or N"
fi

 


And (&&), Or (||)

Double && characters can be used to link statements together as an and.

if [ $foo -eq 1 ] && [ $bar -eq 1 ]

 

Double || characters can be used to link statements together as an or.

if [ $foo -eq 1 ] || [ $bar -eq 1 ]

 

 


Variables

#!/bin/bash
foo=1
if [ $foo -eq 1 ]
then
  value=0
else
  value=1
fi

if [ $value -eq 0 ]
then
  echo "Value is zero"
fi

 


String begins with

Double brackets are used to determine if a string begins with a certain set of characters.

foo=hello

if [[ $foo =~ ^hel ]]
then
  echo "Success"
fi

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments