Cybersecurity Updates & Tools

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