← Back to Bash Course | Chapter 4: Loops | Lesson 9 of 14

Comparison Operators

In this page:

  1. Comparison Operators

Comparison Operators

Inside [[ ]] or (( )), numeric comparisons use -eq -ne -lt -le -gt -ge (in [[ ]]) or the familiar == != < <= > >= (in (( ))). Bash keeps numeric comparison syntax separate from string comparison to avoid ambiguity. Mixing up -eq (numeric) with == in the wrong context is a common beginner mistake.

Warning: Using == inside [ ] (single brackets) works for strings, but for numbers you must use -eq, not ==.

Example: Comparison Operators

bash
#!/bin/bash
a=5
b=8

if [ "$a" -lt "$b" ]; then
    echo "$a is less than $b"
fi

if (( a < b )); then
    echo "Using (( )): $a is less than $b"
fi

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.