Logical operators in Bash Scripting | 2022
Logical operators in Bash Scripting | 2022
Introduction
A logical operator or also called boolean operator is a symbol or word used to connect two or more relational expressions, Common logical operators include AND, OR, and NOT. These operators return a boolean value either TRUE or FALSE. Logical operators are very important in any programming language and they help us make decisions based on certain conditions.
List of logical operators
Logical AND (&&): This operator returns true if both the operands are true otherwise returns false.
Logical OR (||): This operator returns true is either of the operand is true or both the operands are true and return false if none of then is false.
Not Equal to (!): This operator returns true if the operand is false and returns false if the operand is true.
Logical OR (||): This operator returns true is either of the operand is true or both the operands are true and return false if none of then is false.
Not Equal to (!): This operator returns true if the operand is false and returns false if the operand is true.
An object on which we are performing the Operations is called an Operand.
Using AND and OR operator
AND and OR operators are simple to use, you can easily understand them using the below example.
#!/bin/bash
# Reading input from the user
read -p 'Enter your age : ' age
if(($age >= 18 & $age <= 100 ))
then
echo "You can drive"
elif(($age <= 18 || $age >= 100 ))
then
echo "We recommend you not to drive"
fi
# Reading input from the user
read -p 'Enter your age : ' age
if(($age >= 18 & $age <= 100 ))
then
echo "You can drive"
elif(($age <= 18 || $age >= 100 ))
then
echo "We recommend you not to drive"
fi
As in the above example, we have taken a very simple example of Age.
Output:-
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter your age : 83
You can drive
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter your age : 18
You can drive
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter your age : 17
We recommend you not to drive
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter your age : 101
We recommend you not to drive
NOT operator:-
NOT operator is a pretty interesting operator, it turns TRUE into FALSE and FALSE into TRUE.
#!/bin/bash
# Reading input from the user
read -p 'Enter a number : ' number
if(( ! ($number % 2 == 0) ))
then
echo "Odd Number"
else
echo "Even Number"
fi
# Reading input from the user
read -p 'Enter a number : ' number
if(( ! ($number % 2 == 0) ))
then
echo "Odd Number"
else
echo "Even Number"
fi
As the above is a fairly simple example, we are just checking if the number entered by the user is EVEN or ODD. we used basic maths here
($number % 2 == 0)
If you just look at this, it will give us the Even number if we directly used it in the IF condition, as it's checking the reminder, but we have used a ! (exclamation mark) before the statement which is a NOT operator which turns TRUE into FALSE and FALSE into TRUE.
Output:-
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a number : 4
Even Number
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a number : 5
Odd Number
└─$ bash bashscript.sh
Enter a number : 4
Even Number
┌──(kali㉿kali)
└─$ bash bashscript.sh
Enter a number : 5
Odd Number
Conclusion:-
- We learned what are logical operators in Bash Scripting
- We saw the difference between AND, OR, and NOT operators.
- We used logical operators using examples.