Operators perform operations on variables/values. The relational operators in Linux are used to determine relations between two variables/values.
Note: The relational operators only work with numeric values. For example, we can use them with (10, 20, "10", "20") and not the strings ("ten", "twenty").
The relational operators are used in the conditionals, such as the if
condition in shell scripting. Let's check out the operators along with an example.
#!/bin/sha=9b=10if [ $a -eq $b ]thenecho "[$a -eq $b]: The condition passed as a is equal to b"elseecho "[$a -eq $b]: The condition failed as a is not equal to b"fiif [ $a -ne $b ]thenecho "[$a -ne $b]: The condition passed as a is not equal to b"elseecho "[$a -ne $b]: The condition failed as a is equal to b"fiif [ $a -gt $b ]thenecho "[$a -gt $b]: The condition passed as a is greater than b"elseecho "[$a -gt $b]: The condition failed as a is not greater than b"fiif [ $a -lt $b ]thenecho "[$a -lt $b]: The condition passed as a is less than b"elseecho "[$a -lt $b]: The condition failed as a is not less than b"fiif [ $a -ge $b ]thenecho "[$a -ge $b]: The condition passed as a is greater than or equal to b"elseecho "[$a -ge $b]: The condition failed as a is not greater than or equal to b"fiif [ $a -le $b ]thenecho "[$a -le $b]: The condition passed as a is less than or equal to b"elseecho "[$a -le $b]: The condition failed as a is not less than or equal to b"fi
Note: You can change the values of
a
andb
in the example and try out different use cases.
In the example above, we use the if-else conditions in shell scripting and the relational operators.
-eq
operatorLines 6–11: These lines show the use of the -eq
operator. The -eq
operator validates the values in two variables, and the condition becomes true if they are equal; otherwise, it fails. Then we print the statements accordingly.
-ne
operatorLines 13 –18: These lines show the use of the -ne
operator. The -ne
operator validates the values in two variables, and the condition becomes true if they are not equal; otherwise, it fails. Then we print the statements accordingly.
-gt
operatorLines 20–25: These lines show the use of the -gt
operator. The -gt
operator validates the values in two variables, and the condition becomes true if the first variable is greater than the second; otherwise, it fails. Then we print the statements accordingly.
-lt
operatorLines 27 –32: These lines show the use of the -lt
operator. The -lt
operator validates the values in two variables, and the condition becomes true if the first variable is smaller than the second; otherwise, it fails. Then we print the statements accordingly.
-ge
operatorLines 34–39: These lines show the use of the -ge
operator. The -ge
operator validates the values in two variables, and the condition becomes true if the first variable is greater than or equal to the second; otherwise, it fails. Then we print the statements accordingly.
-le
operatorLine 41–46: These lines show the use of the -le
operator. The -le
operator validates the values in two variables, and the condition becomes true if the first variable is less than or equal to the second; otherwise, it fails. Then we print the statements accordingly.