Skip to content
Technology News Information & Jobs

Technology News Information & Jobs

Ubuntu Tutorials, Ubuntu How To , Linux Tips, Tech Jobs

  • Work with US
  • Search Jobs
  • Post a Job
  • Contact Us
  • Ubuntu
    • Ubuntu 18.04
  • Ubuntu 20.04
  • Ubuntu 22.04
  • Themes
  • Linux
  • Laptops
  • Tutorials
  • Job Dashboard
  • Toggle search form
10+ Bash: If, Else If, Else Examples

10+ Bash: If, Else If, Else Examples

Posted on February 18, 2023June 21, 2023 By admin

Bash: If, Else If, Else Examples

In this Bash tutorial post, we will teach you bash script with the IF, ELIF, and ELSE commands with 10+ examples.

10+ Bash: If, Else If, Else Examples

Here are the 10 bash examples:

1. Simple If Statement in bash:

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
fi

2. If-else statement in bash:

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
else
echo "Number is less than or equal to 10"
fi

3. If-else-if statement in bash:

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
elif [ $num -eq 10 ]; then
echo "Number is equal to 10"
else
echo "Number is less than 10"
fi

4. Nested if statement in bash:

if [ $num -gt 10 ]; then
if [ $num -lt 20 ]; then
echo "Number is between 10 and 20"
fi
fi

5. Using logical operators in bash:

if [ $num -gt 10 ] && [ $num -lt 20 ]; then
echo "Number is between 10 and 20"
fi

6. Checking file existence in bash:

if [ -e /path/to/file ]; then
echo "File exists"
else
echo "File does not exist"
fi

7. Checking if a variable is empty in bash:

if [ -z "$var" ]; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi

8. Using a case statement in bash:

case $fruit in
apple)
echo "Fruit is apple"
;;
orange)
echo "Fruit is orange"
;;
*)
echo "Fruit is not apple or orange"
;;
esac

9. Using command substitution in bash:

if [ $(ls /path/to/dir | wc -l) -gt 0 ]; then
echo "Directory is not empty"
else
echo "Directory is empty"
fi

10. Using arithmetic expressions in bash:

if (( $num > 10 )); then
echo "Number is greater than 10"
elif (( $num == 10 )); then
echo "Number is equal to 10"
else
echo "Number is less than 10"
fi

3 examples of If statement in bash on string equality

Using double brackets and == operator:

READ More Relevant Stuff:  How To Fix Busybox Initramfs Error On Ubuntu [2023]

In this example, we use double brackets and the == operator to test for string equality. The advantage of using double brackets is that it is more flexible and can handle more complex conditions.

if [[ "$str1" == "$str2" ]]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi

Using the test command and = operator: 

In this example, we are using the test command with the = operator to test for string equality. The test command is equivalent to the [ command, but requires a trailing semicolon or space before the closing bracket.

if test "$str1" = "$str2"; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi

Using the case statement:

In the below example, we are using a case statement to check if $str1 matches $str2. If the two strings match, the “Strings are equal” message is printed. Otherwise, You will see the “Strings are not equal” message in output. The case the statement allows for more complex matching patterns and can be useful in situations where multiple conditions need to be checked.

case "$str1" in
"$str2")
echo "Strings are equal"
;;
*)
echo "Strings are not equal"
;;
esac

3 examples of If statement in bash on number equality

Using double brackets and == operator:

if [[ "$num1" == "$num2" ]]; then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi

Using the test command and -eq operator:

if test "$num1" -eq "$num2"; then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi

Using arithmetic expression:

if (( num1 == num2 )); then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi

3 example of If statement in bash on less than a number

Using double brackets and -lt operator:

if [[ "$num1" -lt "$num2" ]]; then
echo "$num1 is less than $num2"
else
echo "$num1 is not less than $num2"
fi

Using the test command and -lt operator:

if test "$num1" -lt "$num2"; then
echo "$num1 is less than $num2"
else
echo "$num1 is not less than $num2"
fi

Using arithmetic expression:

if (( num1 < num2 )); then
echo "$num1 is less than $num2"
else
echo "$num1 is not less than $num2"
fi

If statement in bash with logical AND operator

In this example, we are using the logical AND operator && to test if $num1 is greater than 0 AND less than 10. The -gt and -lt operators are used for numeric comparison. If both conditions are true, the message “$num1 is between 0 and 10” is printed. Otherwise, the message “$num1 is not between 0 and 10” will be printed. Note that we are using the separate brackets [ ] for each condition that we want to evaluate.

if [ "$num1" -gt 0 ] && [ "$num1" -lt 10 ]; then
echo "$num1 is between 0 and 10"
else
echo "$num1 is not between 0 and 10"
fi

If statement in bash with logical OR operator

In this example, we use the logical OR operator || to test if $day is equal to “Sunday” OR “Saturday”. If either condition is true, the message ” Yay, It’s the weekend!” is printed. Otherwise, the message “Sadly, It’s a weekday” is printed. Note that we use separate brackets [ ] for each condition that we want to evaluate.

if [ "$day" = "Saturday" ] || [ "$day" = "Sunday" ]; then
echo "Yay, It's the weekend!"
else
echo "Sadly, It's a weekday"
fi

3 Examples of elif condition in bash

Using elif to test for multiple conditions: 

if [ "$num" -gt 0 ]; then
echo "$num is positive"
elif [ "$num" -lt 0 ]; then
echo "$num is negative"
else
echo "$num is zero"
fi

Using elif with a different operator: 

READ More Relevant Stuff:  How To Install Docker On Ubuntu 22.04 LTS [User Guide]

In this example, we are using an if statement to test if $user is not equal to “root”. If the condition is true, the message “You do not have root access” is printed.

If the condition is false, then the  elif statement tests if $user is equal to “root”. If this condition is true, the message “You have root access” is printed.

Note that we are using the != and = operators for string comparison.

if [ "$user" != "root" ]; then
echo "You do not have root access"
elif [ "$user" = "root" ]; then
echo "You have root access"
fi

Using elif with a nested if statement: 

In this example, we are using a if statement to test if $day is equal to “Saturday”. If the condition is true, the message “yay, It’s the weekend!” is printed.

If the condition is false, the elif statement tests if $day is equal to “Sunday”. If this condition is true, the message ” Yay, It’s the weekend!” is printed.

If both conditions are false, the else statement executes a nested if statement that tests if $hour is less than 12. If this condition is true, the message “Good morning!” is printed. If the condition is false, the message “Good afternoon!” is printed.

if [ "$day" = "Saturday" ]; then
echo "Yay, It's the weekend!"
elif [ "$day" = "Sunday" ]; then
echo "Yay, It's the weekend!"
else
if [ "$hour" -lt 12 ]; then
echo "Good morning!"
else
echo "Good afternoon!"
fi
fi

 

How To Ubuntu

Post navigation

Previous Post: How To Install Microsoft Office On Linux 2023
Next Post: 15+BEST Call Recording Apps for Android In 2023 [Comparison]

Related Posts

Basic Cat Command Examples In Linux 15 Basic Cat Command Examples In Linux How To Ubuntu
How To Change Java Version In Linux [2023] How To Change Java Version In Linux [2023] How To Ubuntu
Best Linux Desktop Environment For Ubuntu 22.04 LTS Best Linux Desktop Environment For Ubuntu 22.04 LTS How To Ubuntu
Useful Best GNOME Desktop Extensions In 2021 Useful Best GNOME Desktop Extensions Updated How To Ubuntu
[Fixed] “Temporary failure in name resolution” Issue How To Resolve Temporary failure in name resolution Issue How To Ubuntu
TUXEDO InfinityBook Pro 14 Gen8 Linux Ultrabook: Pre-Order Now TUXEDO InfinityBook Pro 14 Gen8 Linux Ultrabook : Pre-Order Now How To Ubuntu

Recent Posts

  • New Job Search October 10, 2025
  • How To Enable Hibernate in Ubuntu 22.04 LTS May 20, 2024
  • Popular Ubuntu Games: Top 5 Games To Play March 17, 2024
  • WordPress | Your site doesn’t include support for the “generateblocks/container” block GenerateBlocks Plugin February 11, 2024
  • Ubuntu Autoremove Command Debian Autoremove Command February 10, 2024
  • How To Mount Windows 11 Shares on Debian Linux December 7, 2023
  • Anonymous Browsing 101: A Deep Dive Into Residential Proxies November 20, 2023
  • How To Correctly Install Uninstall Linux Ubuntu Nvidia Drivers Fast October 31, 2023
  • Ethical Web Design: The Dos and Don’ts October 28, 2023
  • Understanding the Role of Security Operations Center October 25, 2023
  • The Positive Effects of AI Tools and Online Services on Students Successful Essay-Writing Skills October 3, 2023

Categories

  • Android
  • Apple
  • Blogs
  • Debian
  • Fix
  • Gadgets
  • Game
  • Google
  • How To Ubuntu
  • Internet
  • Laptops
  • Linux
  • Linux Commands
  • Linux Game
  • Linux Mint
  • News
  • Operating Systems
  • Security
  • Technology
  • Themes
  • Tools
  • Tutorials
  • Ubuntu
  • ubuntu 18.04
  • Ubuntu 20.04
  • Ubuntu 22.04
  • Windows 11
  • Wordpress
  • Search Jobs
  • Post a Job
  • Job Dashboard
  • Contact Us
  • About US
  • Cookie Policy
  • Terms and Conditions
  • Privacy Policy
  • Sitemap
  • Write For US

Tags

android apps centos ChatGPT command Commands Debian deepin Desktop docker Fedora fix game games Gaming gnome google kernel linux linux commands linux kernel Linux laptops Linux Mint Linux themes minecraft NVIDIA Python Raspberry Pi sysadmin themes ubuntu ubuntu 18.04 Ubuntu 18.04 LTS Ubuntu 20.04 ubuntu 20.04 lts Ubuntu 22.04 LTS Ubuntu games Ubuntu kernel ubuntu themes ubuntu tutorial VirtualBox Windows Windows 11 Wine WSL
itsubuntu.com is an Equal Opportunity Employer. For Advertisement, Jobs, Guest posts, and any other thoughts, email us at [email protected]
  • Demand Writer – 2402675

    • San Diego, CA
    • JBA International
    • Full Time
  • Blogger

    • Riverside, CA
    • TradeJobsWorkForce
    • Full Time
  • Blogger

    • Bakersfield, CA
    • TradeJobsWorkforce
    • Full Time
  • Copywriter & Copy Editor (Brazilian Jiu-Jitsu & E-commerce Focus)

    • San Diego, CA
    • KINGZ LLC
    • Full Time
  • Automotive Service Writer

    • Charlotte, NC
    • Depo Auto Care
    • Full Time
  • Lead Data Scientist

    • Washington Crossing, PA (Remote)
    • J&J Family of Companies
    • Full Time
  • Freelance Medical Copy Editor (Books, AMA)

    • Saint Louis, MO
    • Graphic World
    • Freelance
  • Service Writer

    • Houston, TX
    • Southern Tire Mart at Pilot LLC
    • Full Time

Copyright © 2025 Technology News Information & Jobs.

Powered by PressBook Grid Blogs theme