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.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | 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
| Operator | Meaning | Example |
|---|---|---|
-eq | Equal to | [ $a -eq $b ] |
-ne | Not equal to | [ $a -ne $b ] |
-gt | Greater than | [ $a -gt $b ] |
-lt | Less than | [ $a -lt $b ] |
-ge | Greater than or equal to | [ $a -ge $b ] |
-le | Less 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
| Operator | Meaning |
|---|---|
= | Strings are equal |
!= | Strings are not equal |
-z | String is empty (zero length) |
-n | String 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.
| Operator | Meaning | Example |
|---|---|---|
-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.
| Operator | Meaning |
|---|---|
-e file | File exists |
-f file | File exists and is a regular file |
-d file | File is a directory |
-r file | File is readable |
-w file | File is writable |
-x file | File is executable |
-s file | File 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
| Operator | Meaning | Equivalent |
|---|---|---|
x=5 | Assign value | x = 5 |
((x+=3)) | Add and assign | x = x + 3 |
((x-=2)) | Subtract and assign | x = x - 2 |
((x*=4)) | Multiply and assign | x = x * 4 |
((x/=2)) | Divide and assign | x = 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,-ltetc. for numeric comparisons inside[ ]. - Use
=and!=for string comparisons. - Use
&&(AND),||(OR), and!(NOT) for logical operations. - Use
-f,-d,-eetc. to check file and directory properties.
