Shell Programming - Relational Operators

Unix

Share

In this tutorial we will learn about Relational Operators in Shell Programming.

Relational operators works with numerical values and with strings that are numbers.

We will discuss the following relational operators in this tutorial.

  • Equal to -eq
  • Not equal to -ne
  • Greater than -gt
  • Less than -lt
  • Greater than or equal to -ge
  • Less than or equal to -le

Note! We will be using if else statement in this tutorial.

Equal to -eq

In the following example we will check if the two numbers are equal.

#!/bin/sh

# take two numbers from the user
echo "Enter two numbers: "
read a b

# check
if [ $a -eq $b ]
then
  echo "Numbers are equal."
else
  echo "Not equals."
fi

Don't forget to give space after [ and before ] like [ $a -eq $b ].

Output:

$ sh equal.sh 
Enter two numbers: 
10 20
Not equals.

$ sh equal.sh 
Enter two numbers: 
100 100
Numbers are equal.

We can also use [ $a == $b ]

Not equal to -nq

In the following example we will check if the two numbers are not equal.

#!/bin/sh

# take two numbers from the user
echo "Enter two numbers: "
read a b

# check
if [ $a -ne $b ]
then
  echo "Numbers are not equal."
else
  echo "Numbers are equal."
fi

Output:

$ sh notequal.sh 
Enter two numbers: 
10 20
Numbers are not equal.

$ sh notequal.sh 
Enter two numbers: 
10 10
Numbers are equal.

We can also use [ $a != $b ]

Greater than -gt

In the following example we will check if entered number is greater than 10.

#!/bin/sh

# take a numbers from the user
echo "Enter a number: "
read a

# check
if [ $a -gt 10 ]
then
  echo "Greater than 10."
else
  echo "Not greater than 10."
fi

Output:

$ sh greaterthan.sh 
Enter two numbers: 
9
Not greater than 10.

$ sh greaterthan.sh 
Enter two numbers: 
11
Greater than 10.

Less than -lt

In the following example we will check if entered number is less than 10.

#!/bin/sh

# take a numbers from the user
echo "Enter a number: "
read a

# check
if [ $a -lt 10 ]
then
  echo "Less than 10."
else
  echo "Not less than 10."
fi

Output:

$ sh lessthan.sh 
Enter a number: 
10
Not less than 10.

$ sh lessthan.sh 
Enter a number: 
9
Less than 10.

Greater than or equal to -ge

In the following example we will check if entered number is greater than or equal to 10.

#!/bin/sh

# take a numbers from the user
echo "Enter a number: "
read a

# check
if [ $a -ge 10 ]
then
  echo "Greater than or equal to 10."
else
  echo "Less than 10."
fi

Output:

$ sh greaterthan-or-equalto.sh 
Enter a number: 
10
Greater than or equal to 10.

$ sh greaterthan-or-equalto.sh 
Enter a number: 
9
Less than 10.

Less than or equal to -le

In the following example we will check if entered number is less than or equal to 10.

#!/bin/sh

# take a numbers from the user
echo "Enter a number: "
read a

# check
if [ $a -le 10 ]
then
  echo "Less than or equal to 10."
else
  echo "Greater than 10."
fi

Output:

$ sh lessthan-or-equalto.sh 
Enter a number: 
10
Less than or equal to 10.

$ sh lessthan-or-equalto.sh 
Enter a number: 
12
Greater than 10.
Share

Recently Updated