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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

10 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

11 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

11 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

11 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

1 day ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

1 day ago