Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash if..else Statement - DesignLinux

Oct 13 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

Wall command in Linux

Prev Post

How to Install Drupal on Debian 10

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 480
  • 612,659

DesignLinux.com © All rights reserved

Go to mobile version