BASH Operators [Part 5]

In the previous decisions article, we came across many new bash operators while learning the if, if-else, and elif statements. This article will provide deep insight to bash operators and their various types in computer programming.

Types of BASH operators

There are many types of BASH operators but for the sake of this series scope, we will focus on those that are beneficial to us. Below are three types of operators in BASH:  

Assignment

The assignment operator is used to store the data on the right-hand side to a variable on the left-hand side. This operator is equal to (=) sign and below is an example demonstrating constant value 2 stored to a variable named x –

x=2

In the variable article, the example programs we practiced all used this equal to assignment operator. So you can be guaranteed that you already know this operator pretty well.

Relational

relational statements in bash script

Relational operators are those operators that are used to make comparisons between two operands (variable contents/data). Below is a list of the various relational operators in BASH:

==

This is equal to the operator that test for equality between two operands or between an operand and an expression. So literally, this operator tests for what’s on the right-hand side must be equal to what’s on the left-hand side. We’ve used this operator a lot in the previous decisions article.

!=

This is the opposite of equal to operator in that it tests for non-equality between two operands. So what’s on the right-hand side must not be equal to what’s on the left-hand side. Below is an example program demonstrating not equal to operator.

!/bin/bash
echo Enter your age:
read age
if [[ $age != 18 ]] ; then
    echo You are not an eighteen year old.
fi

-eq and -ne

-eq (equal to) and -ne (not equal to) are specially defined operators for the if statement and can be used to compare numbers. They function the same as the above two mentioned (== and !=) operators but are best used for comparing against numerical values. Below is a sample script demonstrating -eq and -ne.

#!/bin/bash
echo Enter your age:
read age
# equal to 18
if [[ $age -eq 18 ]] ; then
    echo You are an eighteen years old.
fi
# not equal to 18
if [[ $age -ne 18 ]] ; then
    echo You are not an eighteen year old.
fi

-gt and -lt

Sometimes we want to test if a number exceeds or precedes from a specific point. Such as in the case of our previous voting eligibility program where we had to make sure a user is above 18 years old in order to be eligible to vote. We used the -gt (greater than) operator to make our program intelligent.

if [[ $age == 18 || $age -gt 18 ]] ; then
    echo You are eligible for voting.

Similarly, -lt means less than and is used to verify a number is not exceeding the specified value.  

Logical

logical operators in bash script

Another important operator type is the logical operator in BASH. It mainly deals with boolean values: true and false. Those boolean values are generated when we use relational operators in conditional statements like if and elif.  

&&

Logical AND operator is used when two or more boolean values compared must be true. Otherwise, false value is returned. Below is a sample script demonstrating the && operator. It asks for user age as input and checks if the age is between 12 and 20 to inform the user as a teenager.

#!/bin/bash
echo Enter your age:
read age
if [[ $age -gt 12 && $age -lt 20 ]] ; then
    echo You are a teen.
fi

||

Logical OR operator is somewhat less strict than AND operator. If two or more boolean values compared have a true value then it returns true immediately. Otherwise, all false values will return false. This operator is used in the previous chapter where we demonstrated the voting eligibility program.

if [[ $age == 18 || $age -gt 18 ]] ; then
    echo You are eligible for voting.

So if the user inputs 18, the first part is true since 18 equals 18 but the second part is false because 18 is not greater than 18. However, we already have a true value so the logical OR operator returns true. Same concepts apply conversely when 21 is given as input for the user’s age. But if we enter 17, both results in false, thus, the logical OR returns false and the user is not eligible to vote.  

Conclusion

In the next article, we will be implementing more scripts using the above operators. Be sure to check this article when you get lost or confused. Until then, keep practicing 🙂

SHARE THIS POST

MassiveGRID Banner

Leave a Reply

Your email address will not be published. Required fields are marked *