Boolean Operators in Bash Script

Boolean operators are an essential part of Bash scripting in Linux, and they allow users to combine and manipulate different conditions in logical expressions. The most used Boolean operators in Bash scripting are AND, OR, and NOT. Understanding these operators is crucial for anyone who wants to write efficient and effective Bash scripts in Linux. 

In this article, we will explain Boolean operators in detail, along with examples used in Bash Script Linux.

  • Understanding Boolean Operators
  • AND Operator
  • OR Operator
  • NOT Operator

Understanding Boolean Operators

Boolean operators are logical operators that evaluate either true or false. They are used to combine conditions in logical expressions and perform actions based on the result of those expressions. The three primary Boolean operators are AND, OR, and NOT.

AND Operator

The AND operator evaluates to true only if both conditions connect evaluate to true. The syntax for the AND operator in Bash scripting is “&&”. Here’s an example:

#!/bin/bash
# Initialize variables
var1=10
var2=15
# Check if both conditions are true using the Boolean AND operator
if [ $var1 -eq 10 ] && [ $var2 -lt 20 ]
then
  echo "Both conditions are true"
fi

In this code the echo command will print “Both conditions are true” if the value of var1 is equal to 10 and the var2 value is less than 15 and the output can be seen below:

$ bash and_operator.sh

The same code can be executed by taking input from the user by writing the below code:

#!/bin/bash
read -p "Enter the value of var1: " var1
read -p "Enter the value of var2: " var2
if [ $var1 -eq 10 ] && [ $var2 -lt 15 ]
then
  echo "Both conditions are true"
else
echo "Conditions are false"
fi

In this example, the script checks if the value of $var1 is equal to 10 and if the value of $var2 is less than 15. If both conditions are true, the script executes the echo command, and the output can be seen in the image below.

$ bash and_operator.sh

OR Operator

The OR operator evaluates to true if at least one of the conditions it connects evaluates to true. The syntax for the OR operator in Bash scripting is “||”. Here’s an example:

#!/bin/bash
var1=10
var2=15
if [ $var1 -eq 10 ] || [ $var2 -lt 20 ]
then
  echo "At least one condition is true"
fi

In this example, the script checks if the value of $var1 is equal to 10 or if the value of $var2 is less than 20. If at least one of the conditions is true, the script executes the echo command and the output can be seen below by executing the script.

$ bash or_operator.sh

The same code can be executed by taking input from the user by writing the below code:

#!/bin/bash
read -p "Enter the value of var1: " var1
read -p "Enter the value of var2: " var2
if [ $var1 -eq 10 ] || [ $var2 -lt 20 ]
then
  echo "At least one condition is true"
else
echo "No  condition is True"
fi

The output can be seen by executing the bash script below:

$ bash or_operator.sh

NOT Operator

The NOT operator negates the result of a condition. The syntax for the NOT operator in Bash scripting is “!”. Here’s an example:

#!/bin/bash
var1=5
if ! [ $var1 -eq 10 ]
then
  echo "The condition is false"
fi

In this example, the script checks if the value of $var1 is not equal to 10. If the condition is false, the script executes the echo command and the output can be seen below by executing this script:

$ bash not_operator.sh

Combining Boolean Operators

You can combine Boolean operators to create more complex logical expressions. For example, you can use parentheses to group conditions and create more complicated expressions. Here’s an example:

#!/bin/bash
var1=10
var2=15
var3=35
if [ $var1 -eq 10 ] && ( [ $var2 -lt 20 ] || [ $var3 -gt 30 ] )
then
  echo "The conditions are true"
fi

In this example, the script checks if the value of $var1 is equal to 10 and if either the value of $var2 is less than 20 or the value of $var3 is greater than 30. If both conditions are true, the script executes the echo command which can be verified by executing the below script:

$ bash boolean_operator.sh

Conclusion

The boolean operators combine and manipulate different conditions in logical expressions. Understanding these operators is crucial for writing efficient and effective Bash scripts. The three primary Boolean operators are AND, OR, and NOT, allowing users to evaluate expressions and perform actions based on the result. These three boolean operators have been discussed in detail in this article.