How to Use OR Operator in Bash Scripting?

In the Bash script, there are various numbers of logical operators. One of them is the OR (boolean) operator represented by “||.” The conditions in the Bash script are checked using the OR operators, which return the result in a “true” or “false.” The further execution of the tasks is then decided based on these results (true or false).

This guide will focus on using the OR operator in the Bash script of Linux.

  • Usage of OR Operator in Bash Script
    • Check the User Age Using OR Operator
    • OR Operator Using Loop
    • Check Condition Using “-o” Operator

OR Operator in Bash Script

The syntax for using the OR operator in the bash script, the following syntax is carried out:

Syntax

Condition 1 || condition 2 || Condition 3........Condition n

Enter Condition 1, condition 2, and separate them by OR (||) operator.

The OR operator returns true when any of the given conditions is true or false when all conditions fail. It is just like the following truth table of OR:

OR Truth Table

Condition 1Condition 2Result (Condition || Condition 2)
True True True 
TrueFalse True 
FalseTrueTrue
FalseFalseFalse

Example 1: Check the Age of the User Using OR Operator

The following script will take the age of the user and print the accordingly:

#!/bin/bash

read -p "Enter Your Age to Check: " age

if [ $age -eq 17 ] || [ $age -lt 17 ];
then
echo "You are Younger! "
else
echo "Your are Elder! "
fi

The script is defined as:

  • The “read” property takes the age from the user in the “age” variable by displaying the message.
  • The “if” conditions are separated by the “||” operator that checks if the entered age is less than or equal to 17.
  • The “then” portion executes the “echo” command if any defined conditions are true.
  • The “else” portion executes the “echo” command if both conditions fail.

Save the above script and exit.

Run the script file (script.sh) in the terminal:

$ bash script.sh

The entered age is “16,” and the message “You are Younger!” is printed on the screen.

Example 2: Print Number Using OR Operator in a Loop

The following script will take the starting and ending numbers from the user and generate numbers in between them:

#!/bin/bash

read -p "Enter Starting Number: " start
read -p "Enter Ending Number: " end

while [[ $start -eq $end || $start -lt $end ]];
do
echo "$start"
((start=start+1))
done

The script is defined as:

  • The “read” property is used to take two numbers (Stating and Ending) in the variable “start” and “end.”
  • The “while” loop with the two conditions separated by OR (||) operator to check if the starting number is less than or equal to the ending.
  • The “do” portion prints the starting number stored in the “$start” variable.
  • The “((start=start+1))” increments in the “$start” variable to continue the loop.

Save the above script and exit.

Execute the script file “script.sh” in the terminal:

$ bash script.sh

The entered starting number is 6 and the ending number is 10. So the generated numbers are 6,7,8,9,10.

Example 3: Check Condition Using “o” Operator

The user can also use the “o” operator instead of a double pipe (||) which is also known as the OR operator in bash. Let’s use it in the below script where a number will be taken from the user and check that the number is even or divisible by 3:

#!/bin/bash

read -p "Enter Number:" num

if [ $((num % 2)) == 0 -o $((num % 3)) == 0 ];
then
    echo "Number is even or can be divided by 3."
else
echo "Number is not even nor divider of 3."
fi

The script is defined as 

  • The “read” property takes the number from the user in the “num” variable by displaying the message.
  • The “if” conditions are separated by the “-o” operator that checks if the entered number is even or divisible by 3.
  • The “then” portion executes the “echo” command if any of the specified conditions are true.
  • The “else” portion executes the “echo” command if both conditions fail.

Save the above script and exit.

Run the script using the bash command:

$ bash script.sh

The output is described as

  • First, the entered number was 5, and the message is printed that the number is not even nor divisible by 3.
  • Secondly, the entered number 2, and the message is printed that the number is even or divisible by 3. 

Conclusion

In the Bash script, the OR (||) is the logical operator examined for checking the conditions. It returns the results as “true” or “false”. The “true” status is returned when any given conditions are true and “false” when all conditions fail. The user can use OR (||) operator in the script or in a loop based on the requirement. Apart from that the “-o” operator can also be utilized to get the OR (||) operator results.

This write-up has illustrated the usage of the OR (||) operator in the Bash script.