New Course: Agentic AI with Python
LinuxBash Shell Scripting

Operators in Shell Scripting

Operators help shell scripts compare values, evaluate conditions, and make decisions. They are commonly used with if, else, and elif statements to control how a script executes.

Shell scripting mainly uses three categories of operators:

  1. Numeric operators : Compare numbers.
  2. String operators : Compare text values.
  3. Logical operators : Combine multiple conditions.

Numeric Operators

Numeric operators are used to compare integer values inside conditions.

OperatorMeaningExample
-eqEqual to[ "$a" -eq 10 ]
-neNot equal to[ "$a" -ne 10 ]
-gtGreater than[ "$a" -gt 5 ]
-ltLess than[ "$a" -lt 5 ]
-geGreater than or equal to[ "$a" -ge 90 ]
-leLess than or equal to[ "$a" -le 50 ]

Example

#!/bin/bash

number=10

if [ "$number" -gt 5 ]; then
    echo "Number is greater than 5"
fi

Output:

Number is greater than 5

Since 10 is greater than 5, the condition evaluates to true.


String Operators

String operators are used to compare text values, such as usernames, filenames, or other input provided by users.

OperatorMeaningExample
= or ==Equal to[ "$a" = "devops" ]
!=Not equal to[ "$a" != "devops" ]
-zString is empty[ -z "$a" ]
-nString is not empty[ -n "$a" ]

Example: Comparing Strings

#!/bin/bash

name="devops"

if [ "$name" = "devops" ]; then
    echo "Match"
else
    echo "Not matching"
fi

Output:

Match

Understanding Empty and Non-Empty Strings

The -z and -n operators are useful when checking whether a user has entered a value.

#!/bin/bash

echo "Enter your username:"
read name

if [ -z "$name" ]; then
    echo "Username cannot be empty"
else
    echo "Welcome, $name"
fi
  • -z returns true when the string is empty.
  • -n returns true when the string contains at least one character.

Important Points

  • Always quote variable expansions in string comparisons, such as "$name". This helps prevent errors when a variable is empty or contains spaces.
  • String comparisons are case-sensitive. For example, "Admin" and "admin" are different strings.
  • Inside [ ], both = and == can be used for string equality in Bash. Using = is portable across POSIX-compatible shells.
  • Do not confuse = in a comparison with variable assignment. Assignment uses name="devops", while comparison uses [ "$name" = "devops" ].

Logical Operators

Logical operators combine multiple conditions in a single decision. They are useful when a script must check more than one requirement before executing a command.

OperatorNameMeaning
&&ANDBoth conditions must be true
||ORAt least one condition must be true
!NOTReverses the result of a condition

AND Operator (&&)

The AND operator returns true only when both conditions are true.

#!/bin/bash

age=19
country="India"

if [ "$age" -ge 18 ] && [ "$country" = "India" ]; then
    echo "Eligible to vote in India"
fi

Output:

Eligible to vote in India

Both conditions are satisfied:

  • Age is at least 18 → True
  • Country is India → True

Therefore, the command executes.

If either condition is false, the overall result is false.

OR Operator (||)

The OR operator returns true when at least one condition is true.

#!/bin/bash

age=19
country="India"

if [ "$age" -gt 20 ] || [ "$country" = "India" ]; then
    echo "Condition satisfied"
fi

Output:

Condition satisfied

Here:

  • Age is greater than 20 → False
  • Country is India → True

Since at least one condition is true, the command executes.

NOT Operator (!)

The NOT operator reverses the result of a condition:

  • True becomes false.
  • False becomes true.
#!/bin/bash

number=5

if ! [ "$number" -eq 10 ]; then
    echo "Number is not equal to 10"
fi

Output:

Number is not equal to 10

The original condition is false because 5 is not equal to 10. The ! operator reverses the result, making it true.

ExpressionResult
! trueFalse
! falseTrue

Linux_Operators_Comparison


Practical Example: Combining Operators

Let's combine string, numeric, and logical operators to create a simple access-checking script.

The script grants access only when the username is admin and the user's age is at least 18.

#!/bin/bash

echo "Enter username:"
read user

echo "Enter age:"
read age

if [ "$user" = "admin" ] && [ "$age" -ge 18 ]; then
    echo "Access granted"
else
    echo "Access denied"
fi
UsernameAgeOutputReason
admin19Access grantedBoth conditions are true
Admin19Access deniedUsername comparison is case-sensitive
sushil20Access deniedUsername does not match
admin15Access deniedAge requirement is not met

Note: This example assumes the user enters a valid integer for age. Input validation is needed before performing numeric comparisons on arbitrary user input.


Quick Reference

CategoryOperatorMeaning
Numeric-eqEqual to
Numeric-neNot equal to
Numeric-gtGreater than
Numeric-ltLess than
Numeric-geGreater than or equal to
Numeric-leLess than or equal to
String= or ==Equal to
String!=Not equal to
String-zEmpty string
String-nNon-empty string
Logical&&AND – both conditions must be true
Logical||OR – at least one condition must be true
Logical!NOT – reverses the result

Summary

  • Numeric operators compare integer values using operators such as -gt, -lt, and -eq.
  • String operators compare text and check whether a string is empty or non-empty.
  • Logical operators combine conditions using AND (&&), OR (||), and NOT (!).
  • String comparisons are case-sensitive.
  • Quote variable expansions, especially when checking user input.
  • Use these operators with conditional statements to control the execution of shell scripts.

Written By: Muskan Garg

How is this guide?

Last updated on