Press "Enter" to skip to content

Compare Variables Example in Linux / Unix Shell Script

Compare Variable example in Linux / Unix shell script.

Examples for integer values comparison

Operator -eq is equal to

if [ “$i” -eq “$j” ]

Operator -ne is not equal to

if [ “$i” -ne “$j” ]

Operator -gt is greater than

if [ “$i” -gt “$j” ]

Operator -ge is greater than or equal to

if [ “$i” -ge “$j” ]

Operator -lt is less than

if [ “$i” -lt “$j” ]

Operator -le is less than or equal to

if [ “$i” -le “$j” ]

Operator < is less than (within double parentheses)

((“$i” < “$j”))

Operator <= is less than or equal to (within double parentheses)

((“$i” <= “$j”)) Operator > is greater than (within double parentheses)

((“$i” > “$j”))

Operator >= is greater than or equal to (within double parentheses)

((“$i” >= “$j”))

Examples for string comparison

Operator = is equal to

if [ “$i” = “$j” ]

Bash shell script example:

#!/bin/bash
i=1
j=2

if [ "$i" -ne "$j" ]
then
echo "$i is not equal to $j"
fi

exit 0