Chapter 55: Bash Operators
Bash Operators
Operators in Bash are the special symbols that let you do comparisons, math, logic, file tests, string checks, and more — basically the “verbs” and “connectors” that make your if-statements, loops, and calculations work.
Bash has several different groups of operators, and each group has its own syntax rules. Mixing them up is one of the most common sources of bugs for beginners (especially confusing [ ] vs (( )) vs [[ ]]).
So let’s go slow, group by group, with tons of real examples you can type right now — and I’ll show you wrong vs correct so you never get confused again.
1. Arithmetic Operators – Math in Bash (( )) or $(( ))
Bash does math inside double parentheses (( )) or $(( )) — this is the modern & recommended way.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Assignment & calculation (( a = 10 + 5 )) # a = 15 (( b = a * 3 )) # b = 45 (( c = b / 2 )) # c = 22 (integer division – no decimal) (( d = 17 % 5 )) # d = 2 (remainder) (( e = 2 ** 3 )) # e = 8 (power) echo $a $b $c $d $e |
Shorthand (very common):
|
0 1 2 3 4 5 6 7 8 9 10 |
count=10 ((count++)) # count = 11 ((count--)) # count = 10 ((count += 5)) # count = 15 ((count *= 2)) # count = 30 |
$(( )) – expression inside string or assignment
|
0 1 2 3 4 5 6 7 8 |
echo "2 + 3 = $((2 + 3))" # 2 + 3 = 5 total=$((price * quantity + tax)) echo "Total bill: ₹$total" |
Operators inside (( )) (no $ needed for variables):
| Operator | Meaning | Example inside (( )) |
|---|---|---|
| + – * / | Add, subtract, multiply, divide | ((result = x + y)) |
| % | Modulo (remainder) | ((rem = 17 % 5)) → 2 |
| ** | Power / exponent | ((power = 2 ** 10)) → 1024 |
| ++ — | Increment / decrement | ((i++)) |
| += -= *= /= %= | Compound assignment | ((score += 10)) |
| > < >= <= == != | Comparisons | (( age >= 18 )) |
2. Comparison Operators – For Numbers & Strings
Here is where most confusion happens — Bash has three different syntaxes depending on context.
A. Numeric comparisons – Best inside (( )) or [[ ]]
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
age=25 # Preferred way – inside (( )) if (( age >= 18 )); then echo "Adult" fi # Also works in [[ ]] with -gt, -lt, etc. if [[ $age -ge 18 ]]; then echo "Adult" fi |
Numeric operators (use these in (( )) or [[ $var -eq … ]]):
| Operator | Meaning | Example in (( )) | Example in [[ ]] |
|---|---|---|---|
| -eq | Equal | (( a == b )) | [[ $a -eq $b ]] |
| -ne | Not equal | (( a != b )) | [[ $a -ne $b ]] |
| -gt | Greater than | (( a > b )) | [[ $a -gt $b ]] |
| -ge | Greater or equal | (( a >= b )) | [[ $a -ge $b ]] |
| -lt | Less than | (( a < b )) | [[ $a -lt $b ]] |
| -le | Less or equal | (( a <= b )) | [[ $a -le $b ]] |
B. String comparisons – Use [[ ]] or [ ]
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
city="Hyderabad" # Modern & safe way – [[ ]] if [[ "$city" == "Hyderabad" ]]; then echo "Home!" fi if [[ "$city" != "Delhi" ]]; then echo "Not Delhi" fi # Pattern matching with == if [[ "$city" == Hyd* ]]; then echo "Starts with Hyd" fi # Length check if [[ -z "$empty" ]]; then echo "Variable is empty" fi if [[ -n "$name" ]]; then echo "Name is not empty" fi |
String operators (use in [[ ]] or [ ]):
| Operator | Meaning | Example |
|---|---|---|
| == | Equal (pattern matching in [[ ]]) | [[ “$a” == “yes” ]] |
| != | Not equal | [[ “$a” != “no” ]] |
| < > | Lexicographical order (ASCII) | [[ “apple” < “banana” ]] |
| -z | String is empty / zero length | [[ -z “$var” ]] |
| -n | String is non-empty | [[ -n “$var” ]] |
| = | Old equal (same as == in [ ]) | [ “$a” = “$b” ] |
Important rule: Use [[ ]] instead of [ ] whenever possible — it’s safer, supports == pattern matching, && || inside, and no word splitting issues.
3. File Test Operators – Check Files & Folders
These are very common in scripts.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
file="report.pdf" dir="photos" if [[ -f "$file" ]]; then echo "It's a regular file" fi if [[ -d "$dir" ]]; then echo "It's a directory" fi if [[ -e "$file" ]]; then echo "File or folder exists" fi |
Most used file test operators:
| Operator | Meaning | Example |
|---|---|---|
| -f | Exists and is regular file | [[ -f script.sh ]] |
| -d | Exists and is directory | [[ -d ~/Documents ]] |
| -e | Exists (file or directory) | [[ -e config.txt ]] |
| -r | Readable by current user | [[ -r secret.key ]] |
| -w | Writable | [[ -w log.txt ]] |
| -x | Executable | [[ -x myscript.sh ]] |
| -s | Size > 0 (not empty) | [[ -s data.csv ]] |
| -h / -L | Symbolic link | [[ -L /etc/alternatives ]] |
| -nt | Newer than | [[ file1 -nt file2 ]] |
| -ot | Older than | [[ backup.tar.gz -ot source/ ]] |
4. Logical Operators – && || !
Combine conditions.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Inside [[ ]] if [[ $age -ge 18 && $city == "Hyderabad" ]]; then echo "Adult from Hyderabad" fi # Short-circuit outside [ ] ping -c 1 google.com &> /dev/null && echo "Internet works!" # Negation if [[ ! -f "$file" ]]; then echo "File not found" fi |
Quick Practice Right Now (Type These!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 1. Math (( result = 15 * 4 + 7 )) echo $result # 2. Numeric compare age=20 (( age >= 18 )) && echo "Adult" || echo "Minor" # 3. String compare name="Rahul" [[ "$name" == "Rahul" ]] && echo "Match!" # 4. File test touch test.txt [[ -f test.txt ]] && echo "File exists" [[ -r test.txt ]] && echo "Readable" # 5. Combined file="script.sh" [[ -f "$file" && -x "$file" ]] && echo "Executable script found!" |
Summary Table – Bash Operators Cheat Sheet
| Category | Best Syntax | Common Operators & Examples |
|---|---|---|
| Arithmetic | (( )) or $(( )) | + – * / % ** ++ — += > < == != |
| Numeric compare | (( )) or [[ -eq -gt … ]] | -eq -ne -gt -ge -lt -le |
| String compare | [[ ]] | == != < > -z -n |
| File test | [[ -f -d … ]] | -f -d -e -r -w -x -s -nt -ot |
| Logical | [[ && |
Got it, boss? Bash operators are the tools that let your script decide, calculate, compare, and check things — master them and your scripts become intelligent.
Any part confusing? Want next:
- “Teacher, explain [[ ]] vs [ ] in detail”
- “Advanced arithmetic with bc or expr”
- “How to use operators in loops & conditions”
- “Common mistakes with operators”
Just say — teacher is ready in Hyderabad! Keep typing small tests with [[ ]] and (( )) — soon operators will feel natural! 🐧🔣➕➖✖️➗ 😄
