variable comparison bash code example

Example 1: greater than in script

-eq
is equal to

if [ "$a" -eq "$b" ]

-ne
is not equal to

if [ "$a" -ne "$b" ]

-gt
is greater than

if [ "$a" -gt "$b" ]

-ge
is greater than or equal to

if [ "$a" -ge "$b" ]

-lt
is less than

if [ "$a" -lt "$b" ]

-le
is less than or equal to

if [ "$a" -le "$b" ]

Example 2: test string equality bash

strval1="Ubuntu"
strval2="Windows"

#Check equality two string variables

if [ $strval1 == $strval2 ]; then
  echo "Strings are equal"
else
  echo "Strings are not equal"
fi

#Check equality of a variable with a string value

if [ $strval1 == "Ubuntu" ]; then
  echo "Linux operating system"
else
  echo "Windows operating system"
fi

Tags:

Misc Example