How To

How To Use Conditional Expressions In Bash Scripts

Introduction

Conditional expressions are used in Bash scripts to test whether something is true or false. They help your script make decisions based on numbers, strings, files, directories, permissions, and command results.

If you are learning Bash scripting, conditional expressions are very important. They are commonly used with if, else, elif, while, and until statements. With conditional expressions, you can check if a file exists, if a string is empty, if a number is greater than another number, or if a directory is available.

Conditional expressions are useful for Linux automation, system administration, cybersecurity checks, backup scripts, log analysis, and monitoring scripts.

What Are Conditional Expressions In Bash?

A conditional expression is a test written inside brackets. Bash checks the condition and returns true or false.

Common syntax:

if [[ condition ]]; then    commandfi

Example:

#!/bin/bashuser="admin"if [[ "$user" == "admin" ]]; then    echo "Admin user detected"fi

Here, Bash checks whether the value of user is equal to admin.

String Conditional Expressions

String conditions are used to compare text values.

#!/bin/bashname="kali"if [[ "$name" == "kali" ]]; then    echo "Name matched"else    echo "Name not matched"fi

Common string tests:

[[ "$a" == "$b" ]]   # strings are equal[[ "$a" != "$b" ]]   # strings are not equal[[ -z "$a" ]]        # string is empty[[ -n "$a" ]]        # string is not empty

Example:

#!/bin/bashread -p "Enter username: " usernameif [[ -z "$username" ]]; then    echo "Username cannot be empty"else    echo "Welcome, $username"fi

Numeric Conditional Expressions

Numeric conditions are used to compare numbers.

Common number tests:

[[ $a -eq $b ]]   # equal[[ $a -ne $b ]]   # not equal[[ $a -gt $b ]]   # greater than[[ $a -lt $b ]]   # less than[[ $a -ge $b ]]   # greater than or equal[[ $a -le $b ]]   # less than or equal

Example:

#!/bin/bashfailed_logins=12if [[ $failed_logins -gt 10 ]]; then    echo "Warning: Too many failed login attempts"else    echo "Login attempts are normal"fi

File And Directory Conditional Expressions

Bash can also check files and directories.

[[ -f "$file" ]]   # file exists[[ -d "$dir" ]]    # directory exists[[ -e "$path" ]]   # file or directory exists[[ -r "$file" ]]   # file is readable[[ -w "$file" ]]   # file is writable[[ -x "$file" ]]   # file is executable

Example:

#!/bin/bashlog_file="/var/log/auth.log"if [[ -f "$log_file" ]]; then    echo "Log file found"else    echo "Log file not found"fi

Using Multiple Conditions

You can combine conditions using && and ||.

#!/bin/bashuser="admin"status="active"if [[ "$user" == "admin" && "$status" == "active" ]]; then    echo "Access allowed"else    echo "Access denied"fi

The && operator means both conditions must be true. The || operator means at least one condition must be true.

Conclusion

Conditional expressions in Bash help scripts make decisions. You can use them to compare strings, numbers, files, directories, and permissions.

For beginners, learning Bash conditional expressions is essential because they are used in almost every practical Bash script. They are useful for automation, cybersecurity checks, system monitoring, backups, and Linux administration tasks

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…

6 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…

18 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…

19 hours ago