How To

Bash Comparison Operators Explained With Examples

Introduction

Bash comparison operators are used to compare values inside Bash scripts. These values can be numbers, strings, files, or command results. If you want your Bash script to make decisions, you must understand comparison operators.

Comparison operators are commonly used with if, else, elif, while, and case statements. They help Bash scripts check conditions such as whether two numbers are equal, a file exists, a string is empty, or one value is greater than another.

For beginners, Bash comparison operators are very important because they are the foundation of decision-making in Bash scripting.

What Are Bash Comparison Operators?

comparison operators allow you to test conditions. Based on the result, the script can perform different actions.

Basic syntax:

if [[ condition ]]; then    commandelse    commandfi

Example:

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

Output:

You are eligible

Numeric Comparison Operators

Numeric comparison operators are used to compare numbers.

OperatorMeaning
-eqEqual to
-neNot equal to
-gtGreater than
-ltLess than
-geGreater than or equal to
-leLess than or equal to

Example:

#!/bin/basha=10b=5if [[ $a -gt $b ]]; then    echo "$a is greater than $b"else    echo "$a is not greater than $b"fi

Output:

10 is greater than 5

String Comparison Operators

String comparison operators are used to compare text values.

OperatorMeaning
==Equal to
!=Not equal to
-zString is empty
-nString is not empty

Example:

#!/bin/bashusername="admin"if [[ "$username" == "admin" ]]; then    echo "Access granted"else    echo "Access denied"fi

Output:

Access granted

Check if a string is empty:

#!/bin/bashname=""if [[ -z "$name" ]]; then    echo "Name is empty"else    echo "Name is not empty"fi

File Comparison Operators

Bash also provides operators to check files and directories.

OperatorMeaning
-fFile exists
-dDirectory exists
-eFile or directory exists
-rFile is readable
-wFile is writable
-xFile is executable

Example:

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

Using Multiple Conditions

You can combine multiple conditions using && and ||.

Example:

#!/bin/bashuser="admin"password="12345"if [[ "$user" == "admin" && "$password" == "12345" ]]; then    echo "Login successful"else    echo "Login failed"fi

The && operator means both conditions must be true.

Conclusion

Bash comparison operators are essential for writing useful Bash scripts. They allow you to compare numbers, strings, files, and directories. By using operators like -eq, -gt, ==, !=, -f, and -d, you can create scripts that make decisions automatically.

Once you understand comparison operators, you can write better scripts for Linux automation, system checks, cybersecurity tasks, file monitoring, and user input validation.

Cyber Defence

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

1 hour ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

2 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

7 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

8 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

9 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

10 hours ago