• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash if..else Statement

Written by Admin, Updated On October 10, 2020
bash, terminal
Bash if..else Statement

The if statement is the most basic concepts of any programming language. In Bash, you can also use if…else, if…elif.else and nested if statements like other programming languages. In this article, we will show you the basics of the Bash if statement and use of it in your shell scripts.

if Statement#

If statement can be used in different forms but the following the most basic form:

if TEST-COMMAND
then
  STATEMENTS
fi

The if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword.

Here, if the result of TEST-COMMAND to True, the STATEMENTS will be execute. If TEST-COMMAND becomes False, the STATEMENTS gets ignored.

It’s a best practice to indent the code and code blocks with blank line. Indentations and blank lines make your code more readable and organized.

Following is the example of basic script to check the number is greater than 100:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The entered number is greater than 100."
fi

Save and close the file. Run it from the command line:

bash example.sh

It will ask you to enter any number. If you will enter 105, the condition becomes true so it will execute the echo command which is given inside.

The entered number is greater than 100.

if..else Statement#

Following is the form of Bash if..else statement:

if TEST-COMMAND
then
  STATEMENTS1
else
  STATEMENTS2
fi

If the result of TEST-COMMAND to True, it will execute the STATEMENTS1. If TEST-COMMAND returns False, the STATEMENTS2 will be execute. You can have only one else clause in the statement.

For example, we will make change in previous example by adding else:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 100 ]]
then
  echo "The entered number is greater than 100."
else
  echo "The entered number is equal or less than 100."
fi

Save the file and run again same as previously. Once you enter the number, the script will display a different message based on whether the number is greater or less/equal to 100.

if..elif..else Statement#

The Bash if..elif..else statement takes the following form:

if TEST-COMMAND1
then
  STATEMENTS1
elif TEST-COMMAND2
then
  STATEMENTS2
else
  STATEMENTS3
fi

If the TEST-COMMAND1 returns True, it will execute the STATEMENTS1. Otherwise it will check the TEST-COMMAND2 and if it returns True, the STATEMENTS2 will be execute. If none of any TEST-COMMAND returns True it will execute the STATEMENTS3.

You can have one or more elif clauses in the statement. The else clause is optional.

The conditions will be test sequentially and once any condition returns True the remaining conditions are not performed. The program control moves to the end of the if statements.

Let’s add an elif clause to the previous script:

#!/bin/bash

echo -n "Enter a number: "
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "The entered number is greater than 100."
elif [[ $VAR -eq 10 ]]
then
  echo "The entered number is equal to 100."
else
  echo "The entered number is less than 100."
fi

Nested if Statements#

In Bash, you also can nest the if statements within if statements. You can place multiple if statement inside another if statement.

The following script will prompt you to enter three numbers and will print the largest number among the three numbers.

#!/bin/bash

echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3

if [[ $VAR1 -ge $VAR2 ]]
then
  if [[ $VAR1 -ge $VAR3 ]]
  then
    echo "$VAR1 is the largest number."
  else
    echo "$VAR3 is the largest number."
  fi
else
  if [[ $VAR2 -ge $VAR3 ]]
  then
    echo "$VAR2 is the largest number."
  else
    echo "$VAR3 is the largest number."
  fi
fi

It will show the output as following:

Enter the first number: 105
Enter the second number: 110
Enter the third number: 98
110 is the largest number.

Multiple Conditions#

You can also use the logical OR and AND operators to check multiple conditions in the if statements.

For example, here is given one script to check the largest number among the three numbers. In this code, we removed the nested if statements, and we’re using the logical AND (&&) operator.

#!/bin/bash

echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3

if [[ $VAR1 -ge $VAR2 ]] && [[ $VAR1 -ge $VAR3 ]]
then
  echo "$VAR1 is the largest number."
elif [[ $VAR2 -ge $VAR1 ]] && [[ $VAR2 -ge $VAR3 ]]
then
  echo "$VAR2 is the largest number."
else
  echo "$VAR3 is the largest number."
fi

Test Operators#

In Bash, following is syntax form of the test command:

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

Below are some of the most commonly used operators:

  • -n VAR – returns True if the length of VAR is greater than zero.
  • -z VAR – True if the VAR is empty.
  • STRING1 = STRING2 – Consider True if STRING1 and STRING2 are equal.
  • STRING1 != STRING2 – It will True if STRING1 and STRING2 are not equal.
  • INTEGER1 -eq INTEGER2 – True if INTEGER1 and INTEGER2 are equal.
  • INTEGER1 -gt INTEGER2 – Check and returns True if INTEGER1 is greater than INTEGER2.
  • INTEGER1 -lt INTEGER2 – if INTEGER1 is less than INTEGER2 it returns True .
  • INTEGER1 -ge INTEGER2 – That will check if INTEGER1 is equal or greater than INTEGER2 and returns True.
  • INTEGER1 -le INTEGER2 – True if INTEGER1 is equal or less than INTEGER2.
  • -h FILE – Check if the FILE exists and is a symbolic link then returns True.
  • -r FILE – Returns True if the FILE exists and is readable.
  • -w FILE – It will True if the FILE exists and is writable.
  • -x FILE – True if the FILE exists and is executable.
  • -d FILE – if the FILE exists and is a directory it returns true.
  • -e FILE – True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
  • -f FILE – True if the FILE exists and is a regular file (not a directory or device).

Conclusion#

In this tutorial, you learned how to use the if, if..else and if..elif..else statements in Bash script.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Wall command in Linux
Next Article   How to Create and Select MySQL Database

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook