• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials Bash Scripting Tutorials

Lesson 3: Bash Control Structures – if else

neo by neo
September 11, 2025
in Bash Scripting Tutorials
0
Lesson 3: Bash Control Structures - if else

Lesson 3: Bash Control Structures - if else

0
SHARES
12
VIEWS
Share on FacebookShare on LinkedIn
Table of Contents
  • Lesson 3: Bash Control Structures – if else
  • Understanding Basic Control Structures in Bash: if-else
  • What is an if-else Statement?
    • Basic Syntax
    • Example
  • Conditions
    • Numeric Comparison
    • String Comparison
    • File Conditions
    • Logical Operators
    • Combining Conditions

Lesson 3: Bash Control Structures – if else

Welcome to Lesson 3: Bash Control Structures – if else. In this lesson, we will explore how to use if statements and their variations to control the flow of your Bash scripts. Conditional structures like if-else allow you to make decisions and execute different commands based on specific conditions. Whether you need to check file existence, compare numbers, or evaluate command outputs, mastering if statements will add flexibility and logic to your scripts. Let’s dive into the world of Bash control structures and see how if-else can transform your scripting!

Understanding Basic Control Structures in Bash: if-else

Control structures are essential in any programming language, allowing you to control the flow of your script based on certain conditions. In Bash scripting, the if-else statement is one of the most fundamental control structures. This article will guide you through the basics of using if-else in Bash.

What is an if-else Statement?

An if-else statement in Bash allows you to execute a block of code if a specified condition is true, and optionally execute another block of code if the condition is false. This helps in making decisions within your script.

Basic Syntax

The basic syntax of an if-else statement in Bash is as follows:

if [ condition ]; then
    # code to execute if condition is true
else
    # code to execute if condition is false
fi

Example

#!/bin/bash

number=10

if [ $number -gt 5 ]; then
    echo "The number is greater than 5."
else
    echo "The number is not greater than 5."
fi

In this example, the script checks if the variabbash ifle number is greater than 5. If the condition is true, it prints “The number is greater than 5.” Otherwise, it prints “The number is not greater than 5.”

Copy example in the file and save it in the folder:

nano if-example.sh 

Make the file executable:

chmod +x if-example.sh

Execute the file:

./if-example.sh

Output:

ADVERTISEMENT
ADVERTISEMENT
The number is greater than 5.

Conditions

In Bash scripting, you can use a variety of conditions within if-else statements to compare values and make decisions. Here are some of the most commonly used conditions:

Numeric Comparison

  • -eq: Equal to
  • -ne: Not equal to
  • -lt: Less than
  • -le: Less than or equal to
  • -gt: Greater than
  • -ge: Greater than or equal to

Example:

number=10

if [ $number -eq 10 ]; then
    echo "The number is equal to 10."
fi

String Comparison

  • =: Equal to
  • !=: Not equal to
  • <: Less than (in ASCII alphabetical order)
  • >: Greater than (in ASCII alphabetical order)
  • -z: String is null (has zero length)
  • -n: String is not null (has non-zero length)
string="hello"

if [ "$string" = "hello" ]; then
    echo "The string is hello."
fi

File Conditions

  • -e: File exists
  • -f: File is a regular file
  • -d: File is a directory
  • -r: File is readable
  • -w: File is writable
  • -x: File is executable
file="/path/to/file"

if [ -e $file ]; then
    echo "The file exists."
fi

Logical Operators

  • &&: Logical AND
  • ||: Logical OR
number=10

if [ $number -gt 5 ] && [ $number -lt 15 ]; then
    echo "The number is between 5 and 15."
fi

Combining Conditions

You can combine multiple conditions using logical operators to create more complex expressions.

number=10

if [ $number -gt 5 ] && [ $number -lt 15 ]; then
    echo "The number is between 5 and 15."
else
    echo "The number is not between 5 and 15."
fi

Lesson 3: Bash Control Structures – if else

defencedev.com


«Previous: Lesson 2: Linux Bash Variables
Next: Lesson 4: Bash Control Structures – loops (for, while, until)

About The Author

neo

See author's posts

ADVERTISEMENT
Previous Post

Lesson 2: Linux Bash Variables

Next Post

Lesson 4: Bash Control Structures – loops (for, while, until)

neo

neo

Related Posts

Lesson 11: Practical Linux Bash Examples and Projects
Bash Scripting Tutorials

Lesson 11: Practical Linux Bash Examples and Projects

Lesson 10: Bash File Operation (Read, Write, Copy)
Bash Scripting Tutorials

Lesson 10: Bash File Operation (Read, Write, Copy)

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples
Bash Scripting Tutorials

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

Lesson 8: Working with Bash Strings
Bash Scripting Tutorials

Lesson 8: Working with Bash Strings

Lesson 7: Bash Functions with Examples
Bash Scripting Tutorials

Lesson 7: Bash Functions with Examples

Lesson 6: Using Bash Command Substitution
Bash Scripting Tutorials

Lesson 6: Using Bash Command Substitution

Next Post
Lesson 4: Bash Control Structures – loops (for, while, until)

Lesson 4: Bash Control Structures – loops (for, while, until)

Recommended

FortiGate Administration Settings

Fortinet FortiGate: Change Default Administration HTTPS and HTTP Port

JPG to WebP Conversion

JPG to WebP Conversion – No Upload Needed 

Install Portainer on Ubuntu 24.04 Using Docker Compose

Install Portainer on Ubuntu 24.04 Using Docker Compose

FortiGate Country Address Object Generator – Block or Allow Access by Country

FortiGate Country Address Object Generator – Block or Allow Access by Country

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Categories

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos
  • Web Tools
ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

Install Portainer on Ubuntu 24.04 Using Docker Compose

Install Portainer on Ubuntu 24.04 Using Docker Compose

FortiGate Country Address Object Generator – Block or Allow Access by Country

FortiGate Country Address Object Generator – Block or Allow Access by Country

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools

© 2025 defencedev.com - All rights reserved.