How to Use Logical OR & AND in Shell Script with Examples

Channel: Linux
Abstract: " 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" el

A logical condition is created, when two or more conditioned produce a single result based on them. Here one condition result may also invert the result of another condition.

Logical OR & AND operations are very useful where multiple conditions are used in our programs (scripts).

  • OR is used between two or multiple conditions. It returns true if any one of conditions returns as true. First condition is always checked but the second condition is checked only if first condition is returned false
  • AND is used between two or multiple conditions. It returns true only if all the conditions returns as true. First condition is always checked but the second condition is checked only if first condition is returned true
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 "Atleast one conditions is true" else echo "Both conditions are failed" fi1234567891011#!/bin/bash read -p "Enter First Numeric Value: " firstread -p "Enter Second Numeric Value: " second if [ $first -le 10 ] || [ $second -gt 20 ]then    echo "Atleast one conditions is true"else    echo "Both conditions are failed"fi

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" fi1234567891011#!/bin/bash read -p "Enter First Numeric Value: "  firstread -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

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" fi1234567891011121314#!/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

Conclusion

In this tutorial, you have learned about using of logical AND (&&) and logical OR (||) operators in a shell script.

Ref From: tecadmin

Related articles