Bash If statement with OR Operator

We can use OR logical operator to join two boolean expressions in the condition of an If statement in Bash scripting.

The syntax to join two simple conditions (boolean expressions) with OR logical operator in If statement’s condition is

if [ condition_1 ] || [ condition_2 ];
then
    statement(s)
fi

Reference Bash If Statement.

Example

In the following script, we check if given number is greater than 10 or even number. Here we have two conditions, and we join these two conditions using OR logical operator.

example.sh

#! /bin/bash
 
num=8

if [ $num -gt 10 ] || [ $((num%2)) -eq 0 ];
then
    echo "Number is greater than 10 or even."
fi

Output

sh-3.2# ./example.sh 
Number is greater than 10 or even.
ADVERTISEMENT

Conclusion

In this Bash Tutorial, we learned how to use OR logical operator in If statement, with the help of syntax and examples.