Operators In BASH – Learn BASH | Part 5

In the previous decisions chapter, we came across many new operators while learning the if, if-else, and elif statements. Today this chapter will provide deep insight to operators and their various types in computer programming. Read on below to learn more about them.

Types of operators

There are many types of operators in BASH 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 chapter, 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 chapter decisions. So we’ll have to skip over the example part and focus on other operators.

<strong>​!=</strong>

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 coming chapters, 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 🙂

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More