Bash if Loop Examples (if then fi, if then elif fi, if then else fi)

In Bash scripting, the comparison-based programming statement plays an important role. The commonly used bash “if” loop includes “if then fi”, “if then else if fi”, and “if then else fi” statements. These statements depend on the return status of the specified conditions either true or false.

 This guide shows the practical implementation of bash “if” loop examples in Linux.

Bash “if then fi” Statement

The “if-then fi” depends on the success or failure status of the logical expression. If the given logical expression is true, it executes the enclosed statement of the keywords “then” and “fi”.

Syntax:

The basic syntax of the “if-then fi” statement is typed here:

if [ condition ]
then
code-statements
fi

In the Syntax:

  • The if statement condition accepts the numeric or string value.
  • The “code-statements show any Linux command, executable shell script, shell statement, etc. 
  • The fi is the ending keyword of the if statement 

Example: 

Create and open the bash script using the nano command: 

$ nano Code.sh
#!/bin/bash
n=8
if [ $n%2==0 ]
then
echo "$n is even"
fi

The above block defines the following line of codes:

  • The “#! /bin/bash” tells the script to be executed in “bash” shell.
  • A variable is declared, i.e., n=8. 
  • In last, the “if-then fi” loop is set, which shows that if the “$n” variable value gives “0” after being divided by 2, then the “echo” command will print the enclosed statement:

Execute the script: 

$ ./Code.sh

The execution of “Code.sh” shows that the “if” condition is satisfied that’s why the “echo” command printed the output.

Bash “if then elif fi” Statement

The “if-then elif fi” is generally the nested form of the simple “if” statement. In this nested loop, the “elif” shows an additional  “else if” statement.  

The bash shell first executes the “if” condition if it fails, then it will execute the second “else if” statement. If no condition is satisfied, then the last nested if loop will be terminated. 

Syntax: 

The basic syntax of the “if-then elif fi” statement is written below:

if [ conditional expression1 ]
then
	statements
else
	if [ conditional expression2 ]
	then
      statements 
fi

The syntax is the same as “if-then fi”, however, with an additional “elif fi” statement.

Example:

In the “if-then elif fi” example, we have a “Program.sh” script containing the following code line:

$ nano Program.sh
#!/bin/bash
read -p "Enter a value: " num
if [ $num -eq 600 ]
then
echo "The value is equal to 600"
elif [ $num -lt 600 ]
then
echo "The value is less than 600"
fi
  • The “read” statement store the entered input into the “num” variable.
  • The  “if” statement shows if the num variable is equal to 600” then the first echo command will be executed.
  • The additional “elif” specifies the condition if “$num is less than 600”, then the last echo command will be run.
  • The “fi” will terminate the nested if loop if no condition will be satisfied.

Execute the “Program.sh” bash script and see the result depends on the passing value of the user:

$ ./Program.sh

The second “echo” command of the “elif(else if)” statement is executed, showing that the entered “500” value is less than “600”.

Bash “if then else fi” Statement

The “if then else fi” statement is frequently used to test a loop’s true and false conditions. Let’s understand the syntax of the “if then else fi”:

Syntax: 

if [ conditional expression ]
then
statements

else
statements

fi

The shell will execute the “else” statement in case “if” conditions fail. Once the execution of the “else” part is done, then the shell continues the fi statement execution.

Example: 

The “home” directory contains a bash script named “New.sh” as shown in an image:

$ nano New.sh
#!/bin/bash

Color="Pink"

if [ $Color = 'Pink' ]
    then
      echo "My favorite color is $Color"
else
      echo "My favorite color is not $Color"

    fi

In the above code following parameters are set:

  • The Color variable stores the string value “Pink”
  • The “if” defines the condition that if the Color variable is equal to “Pink”, then first echo statement will print its output.
  • The “else” statement contains another “echo” command that will be executed when the “if” condition fails:

The execution of “New.sh” shows the following result:

$ ./New.sh

The “if” condition satisfies, and the first “echo” command is executed.

Conclusion

In bash scripting, the “if” loop allows the running of more than one conditional statement at the same time. The “if” loop can be utilized simply in the form of “if-then fi” and “if then else fi”. It is also beneficial to run a large no of block statements based on the logical expression as a nested “if” loop, i.e., “if then else if fi”. This guide has explained all the variants of if statements in Bash with examples.