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.

Bash

Shorthand (very common):

Bash

$(( )) – expression inside string or assignment

Bash

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 [[ ]]

Bash

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 [ ]

Bash

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.

Bash

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.

Bash

Quick Practice Right Now (Type These!)

Bash

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! 🐧🔣➕➖✖️➗ 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *