• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to use Conditional Statements on Bash Script

  • 03:22 vim ifelse
  • 05:22 chmod +x ifelse
  • 05:33 ll
  • 05:47 ./ifelse
  • 06:06 ./ifelse
  • 06:23 vim ifelif
  • 09:17 chmod +x ifelif
  • 09:29 ll
  • 09:45 ./ifelif
  • 10:03 ./ifelif
  • 10:22 ./ifelif
  • 10:43 vim nestedif
  • 15:22 chmod +x nestedif
  • 15:34 ll
  • 15:45 ./nestedif
7673

To Use Conditional Statements On Bash Script

Introduction:

The conditional statement is used in any programming language to do any decision-making tasks. This statement is also used in bash to perform automated tasks like another programming language, just the syntax is a little bit different in bash. Bash using two types of conditional statements.

Installation Steps:

Step1: Check the Oracle Linux Version by using the below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.2

Step 2: Next create one script file for if Condition by using Vim command

[root@linuxhelp ~]# vim ifscript
#!/bin/bash
read -p " Enter number : " number
if [ $number -eq 100 ]  ## if the condition is true then run the condition
then
echo "Value is equal to  100"
fi

Step 3: To execute the script first change the executable file permission by using the following command

[root@linuxhelp ~]# chmod +x ifscript

Step 4: To list the file by using the following commands

[root@linuxhelp ~]# ll

total 8
-rw-r--r--. 1 root root    0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root  176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Music
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Videos

Step 5: To execute the script by using the following command

[root@linuxhelp ~]# ./ifscript
 Enter number : 100
Value is equal to  100

Step 6: Again, execute the script by using the following command, if the conditions is not true

[root@linuxhelp ~]# ./ifscript
Enter number : 44
[root@linuxhelp ~]#

Step 7: Next create a one script file for if else Condition by using Vim command

[root@linuxhelp ~]# vim ifelse
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
else
  echo "The variable is equal or less than 10."
fi

Step 8: To execute the script first change the executable file permission by using the following command

[root@linuxhelp ~]# chmod +x ifelse

Step 9: To list the file by using the following commands

[root@linuxhelp ~]# ll
total 12
-rw-r--r--. 1 root root    0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root  175 Sep 13 15:09 ifelse
-rwxr-xr-x. 1 root root  176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Music
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Videos

Step 10: To execute the script by using the following command. If the first condition is true, it will be the output

[root@linuxhelp ~]# ./ifelse
Enter a number: 40
The variable is greater than 10.

Step 11: Again, execute the script by using the following command. If condition is false, it will be the output

[root@linuxhelp ~]# ./ifelse
Enter a number: 2
The variable is equal or less than 10.

Step 12: Next create a one script file for if elif Condition by using Vim command

[root@linuxhelp ~]# vim ifelif
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]]
then
  echo "The variable is equal to 10."
else
  echo "The variable is less than 10."
fi

Step 13: To execute the script first change the executable file permission by using the following command

[root@linuxhelp ~]# chmod +x ifelif

Step 14: To list the file by using the following commands

[root@linuxhelp ~]# ll
total 24
-rw-r--r--. 1 root root  175 Sep 13 15:12 @
-rw-r--r--. 1 root root    0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root  232 Sep 13 15:47 ifelif
-rwxr-xr-x. 1 root root  175 Sep 13 15:09 ifelse
-rwxr-xr-x. 1 root root  176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Music
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Videos

Step 15: To execute the script by using the following command. If the first condition is true, it will be the output

[root@linuxhelp ~]# ./ifelif
Enter a number: 100
The variable is greater than 10.

Step 16: To execute the script by using the following command. If the Second condition is true, it will be the output

[root@linuxhelp ~]# ./ifelif
Enter a number: 10
The variable is equal to 10.

Step 17: To execute the script by using the following command. If the first and second condition is false, it will be the output

[root@linuxhelp ~]# ./ifelif
Enter a number: 5
The variable is less than 10.

Step 18: Next create a one script file for Nested if Condition by using Vim command

[root@linuxhelp ~]# vim nestedif
#!/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

Step 19: To execute the script first change the executable file permission by using the following command

[root@linuxhelp ~]# chmod +x nestedif

Step 20: To list the file by using the following commands

[root@linuxhelp ~]# ll
total 24
-rw-r--r--. 1 root root  175 Sep 13 15:12 @
-rw-r--r--. 1 root root    0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root  232 Sep 13 15:47 ifelif
-rwxr-xr-x. 1 root root  175 Sep 13 15:09 ifelse
-rwxr-xr-x. 1 root root  176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Music
-rwxr-xr-x. 1 root root  440 Sep 13 16:16 nestedif
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root    6 Sep 13 11:36 Videos

Step 21: To execute the script by using the following command

[root@linuxhelp ~]# ./nestedif 
Enter the first number:10
Enter the second number:15 
Enter the Thied number:20
20 is the largest number:

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use Conditional Statements on Bash Script (if elif nestedif). Your feedback is much welcome.

Tags:
michael
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

Is shell scripting and bash scripting the same?

A

Bash is the acronym for "Bourne Again Shell", which is also considered to be a subset of shell scripting.
Shell scripting is scripting in any shell and eases the interaction between the user and operating system, while bash scripting can be done only for the bash.

Q

What are the types of Conditional statements?

A

There are various types of conditional statements in Bash:
if statement
if-else statement
if..elif..else statement
Nested

Q

What is if, else, elif?

A

The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False, it checks the condition of the next elif block and so on. If all the conditions are False, the body of else is executed.

Q

How to create a script file?

A

Use Following command

Vim

Q

How to execute the script file?

A

Use Following command
./

Related Tutorials in How to use Conditional Statements on Bash Script

Related Tutorials in How to use Conditional Statements on Bash Script

How to install Xrdp Server (Remote Desktop) on Oracle Linux 8.5
How to install Xrdp Server (Remote Desktop) on Oracle Linux 8.5
Oct 17, 2022
How to install and update OpenSSL on Debian 11.3
How to install and update OpenSSL on Debian 11.3
Oct 21, 2022
How to Install and Configure Mega in Linux
How to Install and Configure Mega in Linux
Jul 19, 2016
How to use Aureport command on Linux
How to use Aureport command on Linux
Nov 28, 2017
How to install Development tools on Linux
How to install Development tools on Linux
Jun 12, 2018
How to Install mod_ssl and SSL certificate on Oracle Linux
How to Install mod_ssl and SSL certificate on Oracle Linux
Dec 30, 2021
How to install Nextcloud on Ubuntu 22.04 version
How to install Nextcloud on Ubuntu 22.04 version
Jun 23, 2023
How to install ClipGrab in Linux
How to install ClipGrab in Linux
Jul 16, 2016

Related Forums in How to use Conditional Statements on Bash Script

Related Forums in How to use Conditional Statements on Bash Script

Linux
jayce class=
shasum command not found
May 5, 2017
Linux
stephan class=
How to list all samba users
Jan 12, 2018
pv command
muhammad class=
pvcreate command not found error
May 9, 2017
Linux
henry class=
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
Apr 25, 2017
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Linux
baseer class=
single command to apply setfacl for multiple user at a time
Jan 23, 2018
Linux
beulah class=
What does mean by 0 0 value in fstab file
Jan 2, 2018
CentOS
mason class=
Error getting authority: Error initializing authority: Could not connect: No such file or directory (g-io-error-quark, 1)
Nov 20, 2018

Related News in How to use Conditional Statements on Bash Script

Related News in How to use Conditional Statements on Bash Script

Anbox, the Android-to-Linux tool the developers have been waiting for
Anbox, the Android-to-Linux tool the developers have been waiting for
Apr 17, 2017
Linus Torvalds stops signing Linux kernel RC tarballs
Linus Torvalds stops signing Linux kernel RC tarballs
May 17, 2017
Capsule8 Launches Linux-Based Container Security Platform
Capsule8 Launches Linux-Based Container Security Platform
Feb 14, 2017
Symantec updates Management console product
Symantec updates Management console product
Nov 22, 2017
Latest Linux driver release feature seven AMD Vega
Latest Linux driver release feature seven AMD Vega
Mar 23, 2017
A Newer and a Faster Window Manager for Tina (Linux Mint 19.2)
A Newer and a Faster Window Manager for Tina (Linux Mint 19.2)
Apr 9, 2019
Microsoft makes its Azure App service now available on Linux Systems
Microsoft makes its Azure App service now available on Linux Systems
Sep 7, 2017
Docker friendly Alpine Linux gets hardened Node.js
Docker friendly Alpine Linux gets hardened Node.js
Apr 19, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luke ?
workbench for debian

I am using workbench in CentOS whereas now I need to use Debian Operating system so could you please help to install and use in Debian?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.