• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
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
May 27, 2025
in Bash Scripting Tutorials
0
Lesson 3: Bash Control Structures - if else

Lesson 3: Bash Control Structures - if else

0
SHARES
4
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

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:

ADVERTISEMENT
./if-example.sh

Output:

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:

ADVERTISEMENT

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

Tags: bash-scripting
ADVERTISEMENT
Previous Post

Lesson 2: Linux Bash Variables

Next Post

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

neo

neo

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

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

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

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

Browse by Category

  • 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

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

  • 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
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.