• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to use Conditional Statements on Bash Script (if, elif)

  • 00:37 lsb_release -a
  • 00:58 vim if.sh
  • 02:24 chmod +x if.sh
  • 02:30 ./if.sh
  • 02:45 su root
  • 02:53 ./if.sh
  • 03:06 exit
  • 03:14 vim if.sh
  • 04:03 ./if.sh
  • 04:17 vim if.sh
  • 05:30 ./if.sh
{{postValue.id}}

To Use Conditional Statements On Bash Script (if, elif)

Introduction

Conditional statements define “if a condition is true, then do that, otherwise do this instead.” Bash conditionals let you write code that performs different tasks based on specified checks. These checks can be based on a simple assessment that results in a true or false result.

Procedure

Step 1: Check the OS version by using the below command

globalt@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step 2: Create a file by using your favourite editor, here I’m using vim editor

globalt@linuxhelp:~$ vim if.sh
Copy the following lines to the file
#!/bin/bash
if [ $UID -eq 0 ]
then
echo "You are root"
fi

Step 3: Give execute permission to the user by using the below command

globalt@linuxhelp:~$ chmod +x if.sh

Step 4: Now run the script from the globalt user by using the below command

globalt@linuxhelp:~$ ./if.sh
There is no output

Step 5: Switch to the root user by using the below command

globalt@linuxhelp:~$ su root
Password:

Step 6: Run the same script form the root user by using the below command

userroot@linuxhelp:/home/globalt# ./if.sh
You are root

Step 7: Exit from the root user by using the below command

root@linuxhelp:/home/globalt# exit
exit

Step 8: Now make some changes in the script by using the below command

globalt@linuxhelp:~$ vim if.sh
Copy the following lines to the file
if [ $UID -eq 0 ]
then
        echo "You are root"
else
        echo "You are not root user"
fi

Step 9: Again run the script from the globalt user by using the below command

globalt@linuxhelp:~$ ./if.sh
You are not root user

Step 10: Now make some more changes in the script by using the below command

globalt@linuxhelp:~$ vim if.sh
Copy the following lines to the file
if [ $UID -eq 0 ]
then
        echo "You are root"
elif [ $UID -eq 1000 ]
then
        echo "You are globalt user"
else
        echo "You are not root user"
fi

Step 11: Again run the script from the globalt user by using the below command

globalt@linuxhelp:~$ ./if.sh
You are globalt user

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). Your feedback is much welcome.

Tags:
michael
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is the if condition in the script?

A

If the condition represents the condition that you want to check

Q

What is the use of the then-in-if condition?

A

Then is used to execute a specific command, if the previous condition is true.

Q

What is the use of the else in the if condition?

A

if the previous conditions are false, execute another command

Q

What is the elif condition in the script?

A

elif condition is used in order to add an additional condition to your statement.

Q

How to give execute permission to the file?

A

Chmod +x

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 Elijah ?
Remote Desktop Connection Has Stopped Working

When accessing my remote machine server using remote desktop on a windows machine I am getting this error

forum (1)

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.