Python Bash Operators: Arithmetic, String & Logical Operators
Bash supports arithmetic expansion via `$(( expression ))`, string comparison operators inside `[[ ]]`, file status test operators, and short-circuit logical boolean operators (`&&`, `||`, `!`).
"Operators are the decision gears in a script: they compute mathematical results, verify whether files exist on disk, and decide whether to proceed or trigger an alert."
Deep Dive: How It Works
Arithmetic Expansion: `$(( 10 + 5 ))`, `$(( x * y ))`, `$(( a % b ))`, `$(( 2 ** 8 ))` (exponentiation). No floating point (use `bc` or `awk` for decimals).
String Operators: `[[ "$a" == "$b" ]]`, `[[ "$a" != "$b" ]]`, `[[ -z "$str" ]]` (empty string test), `[[ -n "$str" ]]` (non-empty test), `[[ "$str" =~ ^[0-9]+$ ]]` (regex match).
File Operators: `-f file` (regular file exists), `-d dir` (directory exists), `-s file` (file exists and size > 0), `-r / -w / -x` (readable/writable/executable).
Syntax Blueprint
SUM=$(( 10 + 20 )) [[ -f "/etc/hosts" ]] && echo "Hosts file exists" [[ -z "$TOKEN" ]] && echo "Error: Missing token" [[ "$NAME" =~ ^[A-Z] ]] && echo "Starts with capital"
Use $(( )) for math and [[ ]] with file/string operators for robust condition testing.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using `<` or `>` for numerical comparison inside single brackets `[ $a > $b ]`.Why it happens: Single `[` treats `>` as a file redirection operator, accidentally creating an empty file named `$b`.
How to fix: Use `(( a > b ))` or `[[ $a -gt $b ]]`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate Math in Bash
Calculate `RESULT=$(( 25 * 4 ))` and echo "Result: $RESULT".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.