Home Linux Unix / Linux – Shell Boolean Operators

Unix / Linux – Shell Boolean Operators

by Anup Maurya
48 minutes read

Shell scripting in Unix and Linux heavily utilizes boolean operators for making conditional decisions. These operators work just like their counterparts in other programming languages and are used to combine the results of commands or expressions.

The following Boolean operators are supported by the Bourne Shell.

Assume variable a holds 10 and variable b holds 20 then −

OperatorDescriptionExample
!This is logical negation. This inverts a true condition into false and vice versa.[ ! false ] is true.
-oThis is logical OR. If one of the operands is true, then the condition becomes true.[ $a -lt 20 -o $b -gt 100 ] is true.
-aThis is logical AND. If both the operands are true, then the condition becomes true otherwise false.[ $a -lt 20 -a $b -gt 100 ] is false.

Example

Here is an example which uses all the Boolean operators −

#!/bin/sh

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a is not equal to b"
else
   echo "$a = $b: a is equal to b"
fi

if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a -lt 100 -a $b -gt 15 : returns true"
else
   echo "$a -lt 100 -a $b -gt 15 : returns false"
fi

if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : returns true"
else
   echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : returns true"
else
   echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

Logical operators are used to combine multiple conditions in a script, allowing for more complex decision-making. The two most commonly used logical operators in shell scripting are Logical OR (||) and Logical AND (&&).

These operators are used in conditional statements like if, while, and until, as well as in command chaining. Command chaining is a technique where multiple commands are executed in a sequence, based on the success or failure of previous commands.

  • The Logical OR operator (||) is used to execute the command following it only if the previous command fails (returns a non-zero exit status).
  • The Logical AND operator (&&) is used to execute the command following it only if the previous command is successful (returns a zero exit status).

Example:

ls /etc && cat /etc/passwd

This command will only print the contents of /etc/passwd if the ls /etc command executes successfully (indicating the directory exists and you have permission to list its contents).

1. Using Logical OR (||) in Shell Scripts

Logical OR in bash script is used with operator -o. Write a small shell script that will show how to use logical OR ( || ) operator between two conditions.

#!/bin/bash
 
read -p "Enter First Numeric Value: " first
read -p "Enter Second Numeric Value: " second
 
if [ $first -le 10 ] || [ $second -gt 20 ]
then
    echo "At least one conditions is true"
else
    echo "Both conditions are failed"
fi

This shell script prompts the user to input two numeric values. It then checks if either the first value is less than or equal to 10, or the second value is greater than 20. If at least one of these conditions is met, the script outputs “At least one condition is true.” Otherwise, it outputs “Both conditions are failed.”

2. Using Logical AND (&&) in Shell Scripts

Logical AND in bash script is used with operator -a. Below shell script will show you to how to use logical AND ( && ) between two conditions.

#!/bin/bash
 
read -p "Enter First Numeric Value: "  first
read -p "Enter Second Numeric Value: "  second
 
if [ $first -le 10 ]  && [ $second -gt 20 ]
then
    echo "Both conditions are true"
else
    echo "Atleast one conditions is false"
fi

This shell script prompts the user to input two numeric values. It then checks if the first value is less than or equal to 10 and the second value is greater than 20. If both conditions are met, the script outputs “Both conditions are true.” Otherwise, it outputs “At least one condition is false.”

3. Using Multiple Logical OR & AND

Now, use the multiple logical operators in a single statement. The below example will help you to understand how to use multiple logical operators in a single statement.

#!/bin/bash
 
# A sample shell script to take input a number from the user
# Check if the number is between 10 - 20
# Or number is between 100 - 200
 
read -p "Enter a number: " num
 
if ([ $num -ge 10 ] && [ $num -le 20 ]) || ([ $num -ge 100 ] && [ $num -le 200 ])
then
     echo "Input number ($num) is between 10-20 or 100-200"
else
     echo "Input number ($num) is neither between 10-20 nor 100-200"
fi

This shell script allows a user to input a number and then checks if the entered number lies within either the range of 10-20 or 100-200. The script then outputs a message to the user indicating whether the input number falls within one of these specified ranges or not.

Additional Points:

  • There are alternative ways to represent AND and OR using the -a and -o operators within square brackets []. These are typically used with the test command.
  • Modern shells often use double square brackets [[ ]] which offer more flexibility, including the ability to use these boolean operators directly within the brackets.

Remember, understanding these operators is essential for writing effective shell scripts that make decisions based on conditions.

related posts

Leave a Comment