How To

Bash If Else Statement Explained For Beginners

Introduction

The if else statement is one of the most important concepts in Bash scripting. It allows a Bash script to make decisions based on conditions. For example, you can check if a file exists, if a user entered the correct password, if a number is greater than another number, or if a command was successful.

If you are learning Bash scripting in Linux, understanding if else conditions is necessary. It is widely used in automation scripts, system administration tasks, cybersecurity checks, backup scripts, and log monitoring.

What Is If Else In Bash?

An if else statement checks a condition. If the condition is true, one block of commands runs. If the condition is false, another block of commands runs.

Basic syntax:

if [[ condition ]]; then    commandelse    commandfi

In Bash, every if statement must end with fi.

Basic Bash If Else Example

Create a new Bash script:

nano if-else.sh

Add the following code:

#!/bin/bashage=18if [[ $age -ge 18 ]]; then    echo "You are eligible"else    echo "You are not eligible"fi

Save the file and run it:

chmod +x if-else.sh./if-else.sh

Output:

You are eligible

Here, -ge means greater than or equal to.

Bash If Else With User Input

You can also use if else with user input.

#!/bin/bashread -p "Enter your username: " usernameif [[ "$username" == "admin" ]]; then    echo "Welcome admin"else    echo "Access denied"fi

This script checks whether the entered username is admin.

Check If A File Exists Using If Else

File checking is a common use case in Bash scripting.

#!/bin/bashfile="/etc/passwd"if [[ -f "$file" ]]; then    echo "File exists"else    echo "File does not exist"fi

The -f operator checks whether the given path is a regular file.

If Elif Else Statement

When you need to check multiple conditions, use elif.

#!/bin/bashmarks=75if [[ $marks -ge 90 ]]; then    echo "Grade A"elif [[ $marks -ge 60 ]]; then    echo "Grade B"else    echo "Grade C"fi

The script checks each condition one by one and runs the matching block.

Conclusion

The if else statement is used to make decisions in scripts. It helps your script respond differently based on conditions such as numbers, strings, files, directories, and user input.

For beginners, learning if else is an important step in Bash scripting. Once you understand it, you can create smarter Linux automation scripts for system checks, cybersecurity tasks, file validation, and user input handling.

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

7 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

17 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

18 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

18 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

19 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

20 hours ago