Operators in Bash

Operators are symbols or keywords that perform operations on values. Bash supports arithmetic, comparison, logical, and string operators. Understanding operators is essential before writing conditional statements and loops.

Arithmetic Operators

Arithmetic operations in Bash are performed using the $(( )) syntax or the expr command.

OperatorMeaningExampleResult
+Addition$((5 + 3))8
-Subtraction$((10 - 4))6
*Multiplication$((6 * 3))18
/Division (integer)$((10 / 3))3
%Modulus (remainder)$((10 % 3))1
**Exponent (power)$((2 ** 4))16
#!/bin/bash
a=10
b=3
echo "Addition:       $((a + b))"
echo "Subtraction:    $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division:       $((a / b))"
echo "Modulus:        $((a % b))"
echo "Power:          $((a ** b))"

Output:

Addition:       13
Subtraction:    7
Multiplication: 30
Division:       3
Modulus:        1
Power:          1000

Comparison (Relational) Operators

Comparison operators compare two values and return true or false. They are used inside if conditions.

Numeric Comparison Operators

OperatorMeaningExample
-eqEqual to[ $a -eq $b ]
-neNot equal to[ $a -ne $b ]
-gtGreater than[ $a -gt $b ]
-ltLess than[ $a -lt $b ]
-geGreater than or equal to[ $a -ge $b ]
-leLess than or equal to[ $a -le $b ]
#!/bin/bash
a=8
b=5
if [ $a -gt $b ]; then
  echo "$a is greater than $b"
fi

Output:

8 is greater than 5

String Comparison Operators

OperatorMeaning
=Strings are equal
!=Strings are not equal
-zString is empty (zero length)
-nString is not empty
#!/bin/bash
fruit="mango"
if [ "$fruit" = "mango" ]; then
  echo "It is a mango."
fi

Output:

It is a mango.

Logical Operators

Logical operators combine multiple conditions.

OperatorMeaningExample
-a or &&AND – both conditions must be true[ $a -gt 0 -a $a -lt 100 ]
-o or ||OR – at least one must be true[ $a -eq 0 -o $b -eq 0 ]
!NOT – reverses the result[ ! -z "$name" ]
#!/bin/bash
marks=75
if [ $marks -ge 50 ] && [ $marks -le 100 ]; then
  echo "Student has passed."
fi

Output:

Student has passed.

File Test Operators

These operators check properties of files and directories. They are very useful in scripts that work with files.

OperatorMeaning
-e fileFile exists
-f fileFile exists and is a regular file
-d fileFile is a directory
-r fileFile is readable
-w fileFile is writable
-x fileFile is executable
-s fileFile is not empty (size > 0)
#!/bin/bash
if [ -f "notes.txt" ]; then
  echo "File notes.txt exists."
else
  echo "File not found."
fi

Assignment Operators

OperatorMeaningEquivalent
x=5Assign valuex = 5
((x+=3))Add and assignx = x + 3
((x-=2))Subtract and assignx = x - 2
((x*=4))Multiply and assignx = x * 4
((x/=2))Divide and assignx = x / 2
#!/bin/bash
x=10
((x += 5))
echo "x is now: $x"

Output:

x is now: 15

Operator Precedence Diagram

┌───────────────────────────────────────────────┐
│         Operator Precedence (High → Low)      │
│                                               │
│  1. ( )        → Parentheses (highest)        │
│  2. **         → Exponent                     │
│  3. * / %      → Multiply, Divide, Modulus    │
│  4. + -        → Add, Subtract                │
│  5. -eq -gt    → Comparison                   │
│  6. !          → NOT                          │
│  7. &&         → AND                          │
│  8. ||         → OR (lowest)                  │
└───────────────────────────────────────────────┘

Key Takeaways

  • Use $(( )) for arithmetic operations.
  • Use -eq, -gt, -lt etc. for numeric comparisons inside [ ].
  • Use = and != for string comparisons.
  • Use && (AND), || (OR), and ! (NOT) for logical operations.
  • Use -f, -d, -e etc. to check file and directory properties.

Leave a Comment