BASH – If statement and comparison operators

Print Friendly, PDF & Email

You can use if statements without any brackets or within [] or [[]] or (). Example of each syntax is given below:

    If statement without any brackets:

These are used to check the outcome of a command. It return the exist status of command executed after if.

if grep -i oolala lyrics.txt
then 
  echo "lyrics.txt file contains oolala"
else
  echo "lyrics.txt file doesn't contain oolala"
fi 

    if statement with []:

These are used to check the file types and compare strings.

if [ -z $variable ]
then
  echo "Variable is empty"
else
  echoo "Variable is not empty"
fi

    if statement with [[]]:

These are used for the same purpose as [] but more versatile and doesn’t give error when &&, ||, >, < are used within these brackets unlike [].

if [[ "$number1" -eq "$number2" ]] 
then
  echo "numbers are equal"
else
  echo "numbers are not equal"
fi
    if statement with no “if”:

These are useful to evaluate a condition and take a single action.

[ -f "$filename" ] || touch $filename # if $filename doesn't exist, it will touch that file
[ -f "$filename" ] && rm $filename    # if $filename exists, it will remove that file

    nested if statement:

if [[ "$fruitname" == "apple" ]]
then
  echo "Fruit is apple"
elif [[ "$fruitname" == "mango" ]] 
then
  echo "Fruit is mango"
else
  echo "Fruit can not be identified"
fi

    Compound if conditions

This is a combination of more than one test expression in if statement. Best explained with an example.

num1=10
num2=20
if [ "$num1" -eq 10 ] && [ "$num2" -eq 20 ]
then
  echo "Number test successful"  => It is successful because both numbers match their values
else
  echo "Number test failed"
fi
if [ "$num1" -eq 10 ] || [ "$num2" -eq 30 ]
then
  echo "Number test successful"  => It is successful because num1 matches 10
else
  echo "Number test failed"
fi
if [ "$num1" -eq 10 -a "$num2" -eq 20 ]
then
  echo "Number test successful"  => It is successful because both numbers match their values
else
  echo "Number test failed"
fi
if [ "$num1" -eq 30 -o "$num2" -eq 20 ]
then
  echo "Number test successful"  => It is successful because num2 matches 20
else
  echo "Number test failed"
fi

fruit1=apple
fruit2=mango
if [ "$fruit1" == apple ] && [ "$fruit2" == mango ]
then
  echo "String test successful"  => It is successful because both strings match their values
else
  echo "String test unsuccessful"
fi

    Test Operators

File test operators:
To be used as:-

if [ "operator" $filename ]; then

-e : Filename exists
-d : Filename exists and is a directory
-f : Filename exists and is a regular file
-s : Filename exists and its size is greater than zero
-b : Filename exists and is a block device
-c : Filename exists and is a character device
-p : Filename exists and is a pipe
-L : Filename exists and is a symbolic Link
-S : Filename exists and is a socker
-g : setgid is set on filename
-u : setuid is set on filename
-k : sticky bit is set on filename
-r : user running the script has read permission on given file
-w : user running the script has write permission on given file
-x : user running the script has execute permission on given file
-O : user running the script is owner of given file
-G : user running the script has same group id as of the script
file1 -nt file2 : File file1 is newer than file2
file1 -ot file2 : File file1 is older than file2
file1 -ef file2 : file1 and file2 are hard links to the same file

String operators:
To be used as:-

 if [ "$string1" <condition> "$string2" ]; then

=  : strings are equal
== : strings are equal
!= : strings are not equal
<  : string1 is less than string2 in ASCII order. 
     if [[ "string1" < "string2" ]] or if [[ "string1" \< "string2" ]
>  : string1 is more than string2 in ASCII order. 
     if [[ "string1" > "string2" ]] or if [[ "string1" \> "string2" ]
-z : string is empty
-n : string is not empty

Numeric Operators:
To be used as:-

if [ "$number1" <condition> "$number2" ]; then

-eq : numbers are equal
-ne : numbers are not equal
-gt : number1 is greater than number2
-ge : number1 is greater than or equal to number2
-lt : number1 is less than number2
-le : number1 is less than or equal to number2
<   : number1 is less than number2. To be used within (()) without the word "if". 
      (( "$num1" < "$num2" ))
<=  : number1 is less than or equal to number2. To be used within (()) without the word "if". 
      (( "$num1" <= "$num2" ))
>   : number1 is greater than number2. To be used within (()) without the word "if". 
      (( "$num1" > "$num2" ))
>=  : number1 is greater than or equal to number2. To be used within (()) without the word "if". 
      (( "$num1" >= "$num2" ))

About sanaswati