#### Bash/Shell scripting - Performing Relational operations using shell scripts Previously we have seen shell scripting for arithmetic operations. In this article we will see shell scripting for relational operations.

Relational Operators :

A Relational operators are those which defines the relation between two operands. They give either true or false depending upon the relation between the operands.

Since they are a decision making statements we need to use if cases such as if,then and else.

They are of 6 types of relational operators. We will see each operator with an example.

Equal to Operator ( -eq ) or ( == ) :

The Equal to operator compares two operands and returns true if they are equal otherwise returns false.

Open a file as equal.sh

\$ vim equal.sh

Paste the below code

Code:

#!/bin/sh

echo "Enter two numbers:"

if [ \$num1 -eq \$num2 ]
then
echo "\$num1 is equal to \$num2"
else
echo "\$num1 is not equal to \$num2"
fi

Run the file,

jhony@ljlinux:~\$ ./equal.sh
Enter two numbers:
10
10
10 is equal to 10

Explanation of the code:

Step 1: Getting the two inputs from the users as num1 and num2.
Step 2: With -eq operator we are comparing the given values.
Step 3: Using if statement will print the given values are equal.
Step 4: Else printing that the values are not equal.

Not equal to Operator ( -ne ) or ( != ) :

The Not Equal to operator returns true if the two operands are not equal otherwise it returns false.

Open a file as notequal.sh

\$ vim notequal.sh

Paste the below code

Code:

#!/bin/sh

echo "Enter two numbers:"

if [ \$num1 -ne \$num2 ]
then
echo "\$num1 is not equal to \$num2"
else
echo "The given values are equal"
fi

Run the file,

jhony@ljlinux:~\$ ./notequal.sh
Enter two numbers:
10
5
10 is not equal to 5

Explanation of the code:

Step 1: Getting the two inputs from the users as num1 and num2.
Step 2: With -ne operator we are comparing the given values.
Step 3: Using if statement will print the given values are not equal.
Step 4: Else printing that the given values are equal.

Less than Operator  ( -lt ) or ( < ) :

Less than operator returns true if an operand is less than next operand otherwise returns false.

Open a file as lessthan.sh

\$ vim lessthan.sh

Paste the below code

Code:

#!/bin/sh

echo "Enter two numbers:"

if [ \$num1 -lt \$num2 ]
then
echo "\$num1 is less than \$num2"
else
echo "\$num2 is less than \$num1"
fi

Run the file,

jhony@ljlinux:~\$ ./lessthan.sh
Enter two numbers:
5
8
5 is less than 8

Explanation of the code:

Step 1: Getting the two inputs from the users as num1 and num2.
Step 2: With -lt operator we are comparing the values.
Step 3: Using if statement will print the num1 as lesser value.
Step 4: Else printing num2 as lesser value.

Less than or equal to Operator ( -le ) or ( <= ) :

Less than or equal to operator returns true if first operand is less than or equal to second operand otherwise returns false

Open a file as lessequal.sh

\$ vim lessequal.sh

Paste the below code

Code:

#!/bin/sh

echo "Enter two numbers:"

if [ \$num1 -le \$num2 ]
then
echo "\$num1 is less than \$num2"
else
echo "\$num2 is less than \$num1"
else
echo "Both numbers are equal"
fi

Run the file,

jhony@ljlinux:~\$ ./lessthan.sh
Enter two numbers:
5
5
Both numbers are equal

Explanation of the code:

Step 1: Getting the two inputs from the users as num1 and num2.
Step 2: With -le operator we are comparing the values.
Step 3: Using if statement will print the num1 as lesser value.
Step 4: Else printing num2 as lesser value otherwise printing both are equal.

Greater than Operator  ( -gt ) or ( > ) :

Greater than operator return true if the first operand is greater than the second operand otherwise return false.

Open a file as lessthan.sh

\$ vim greaterthan.sh

Paste the below code

Code:

#!/bin/sh

echo "Enter two numbers:"

if [ \$num1 -gt \$num2 ]
then
echo "\$num1 is greater than \$num2"
else
echo "\$num2 is greater than \$num1"
fi

Run the file,

jhony@ljlinux:~\$ ./greaterthan.sh
Enter two numbers:
5
8
8 is greater than 5

Explanation of the code:

Step 1: Getting the two inputs from the users as num1 and num2.
Step 2: With -gt operator we are comparing the values.
Step 3: Using if statement will print the num1 as greater value.
Step 4: Else printing num2 as greater value.

Greater than or equal to Operator ( -ge ) or ( >= ) :

Greater than or equal to operator returns true if the operand is greater than or equal to the next operand otherwise returns false

Open a file as greaterequal.sh

\$ vim greaterequal.sh

Paste the below code

Code:

#!/bin/sh

echo "Enter two numbers:"

if [ \$num1 -ge \$num2 ]
then
echo "\$num1 is greater than \$num2"
else
echo "\$num2 is greater than \$num1"
else
echo "Both numbers are equal"
fi

Run the file,

jhony@ljlinux:~\$ ./greaterequal.sh
Enter two numbers:
5
9
9 is greater than 5

Explanation of the code:

Step 1: Getting the two inputs from the users as num1 and num2.
Step 2: With -ge operator we are comparing the values.
Step 3: Using if statement will print the num1 as greater value.
Step 4: Else printing num2 as greater value otherwise printing both are equal.

Things to be remembered:

It is important to define the conditional expressions inside square braces with spaces.

Example: [ \$num1 <= \$num2 ] is the correct format
[\$num1 <= \$num2] is not the right format.

We can either use the operator with alphabetic expression or direct operators( == or -eq ).

That's it for Relational operators. Feel free to ask if you have any queries. 