RELATIONAL OPERATORS OF WHILE LOOP IN LINUX/UNIX SHELL SCRIPT

shivdevops

shiv

Posted on July 23, 2020

RELATIONAL OPERATORS OF WHILE LOOP IN LINUX/UNIX SHELL SCRIPT

Unix / Linux - Shell Relational Operators:

Relational operators that are purely supports numeric values.It does not support any other strings etc.

1. -eq operator

compares whether the value of two operands are equal or not.If true then -eq operator becomes true.

Example:

#!/bin/sh
a=30
b=30

if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi

Output:

30 -eq 30: a is equal to b

2. -ne operator

Compares whether the value of two operands are not equal or not.If true,then -ne operator becomes true.

Example:

#!/bin/sh
a=30
b=50

if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi

Output:

30 -ne 50: a is not equal to b

3. -gt operator

Compares whether the value of left operand is greater than the value of right operand.If true,then -gt operator becomes true.

Example:

#!/bin/sh
a=60
b=30

if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi

Output:

60 -gt 30: a is greater than b

4. -lt operator

Compares whether the value of left operand is less than the value of right operand.If true, then -lt operator becomes true.

Example:

#!/bin/sh
a=40
b=70

if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi

Output:

40 -lt 70: a is less than b

5. -ge operator

Compares whether the value of left operand is greater than or equal to the value of right operand.If True, then -ge operator becomes true.

Example:

#!/bin/sh
a=50
b=20

if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi

Output:

50 -ge 20: a is greater or equal to b

6. -le operator

Compares whether the value of left operand is less than or equal to the value of right operand.If true, then the it becomes true.

Example:

#!/bin/sh
a=40
b=40

if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi

Output:

40 -le 40: a is less or equal to b

💖 💪 🙅 🚩
shivdevops
shiv

Posted on July 23, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related