Bash OR Logical Operator

The Bash OR operator combines conditions or commands when either one may succeed. Bash commonly uses || for logical OR. An OR expression is considered successful when at least one of its operands succeeds.

In conditional statements, || lets a script continue when the condition on the left or the condition on the right is true. Bash also uses || between commands, where the command on the right runs only if the command on the left fails.

Syntax of the Bash OR Operator ||

Following is the syntax of OR logical operator in Bash scripting.

</>
Copy
operand_1 || operand_2

Here, operand_1 and operand_2 may be commands, test expressions, or compound conditions. The || operator represents logical OR. If the left operand succeeds, Bash does not need to evaluate the right operand.

Bash OR Operator Truth Table

The following truth table shows the result of an OR expression for each possible combination of two boolean conditions.

Operand_1Operand_2Operand_1 || Operand_2
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

The combined expression is false only when both operands are false.

Bash OR Operator with [[ ]] Conditions

Inside Bash’s [[ ... ]] conditional expression, multiple tests can be joined with ||. This form is convenient when both comparisons belong to the same condition.

</>
Copy
if [[ condition_1 || condition_2 ]]; then
    commands
fi

For example, the following script prints a message when the value of day is either Saturday or Sunday.

</>
Copy
#!/bin/bash

day="Sunday"

if [[ "$day" == "Saturday" || "$day" == "Sunday" ]]; then
    echo "It is the weekend."
fi

Output

It is the weekend.

Bash OR Operator with Separate [ ] Tests

When using the traditional [ ... ] test command, two complete test commands can be joined with ||.

</>
Copy
if [ condition_1 ] || [ condition_2 ]; then
    commands
fi

This is the form used in the next example.

Bash OR Operator in IF Condition

In the following example, we shall use Bash OR logical operator, to form a compound boolean expression for Bash IF.

We shall check if the number is even or if it also divisible by 5.

Bash Script File

</>
Copy
#!/bin/bash

num=50

if [ $((num % 2)) == 0 ] || [ $((num % 5)) == 0 ];
then
    echo "$num is even or divisible by 5."
fi

Output

50 is even or divisible by 5.

The first test checks whether the remainder after division by 2 is zero. The second checks divisibility by 5. Because at least one condition is true for 50, the compound OR condition succeeds and the echo command runs.

How || Uses Bash Command Exit Status

Bash does not require operands of || to be boolean variables. Commands themselves have an exit status. An exit status of 0 represents success, while a non-zero status represents failure.

When two commands are joined by ||, Bash executes the second command only when the first command fails.

</>
Copy
command_1 || command_2

For example, the following command attempts to change to a directory. If that operation fails, it prints an error message.

</>
Copy
cd /path/to/project || echo "Could not open the project directory."

If cd succeeds, the echo command is skipped. If cd returns a non-zero exit status, Bash evaluates the expression on the right side of ||.

Short-Circuit Evaluation with Bash ||

The Bash OR operator uses short-circuit evaluation. Once the left side of an OR expression succeeds, the complete OR expression is already successful, so Bash does not execute the right side.

</>
Copy
true || echo "This will not run"
false || echo "This will run"

Output

This will run

The true command succeeds, so Bash skips its right-hand command. The false command fails, so Bash executes the corresponding echo command.

Bash OR Operator for File and Directory Tests

The OR operator is also useful when a script should accept more than one file-system condition. The following example checks whether a path is either a regular file or a directory.

</>
Copy
#!/bin/bash

path="example.txt"

if [[ -f "$path" || -d "$path" ]]; then
    echo "The path exists as a file or directory."
else
    echo "The path was not found as a file or directory."
fi

Here, -f tests for a regular file and -d tests for a directory. Only one of these tests needs to succeed for the OR expression to be true.

Bash OR Operator in While Loop Expression

In this example, we shall use Bash OR boolean logical operator in while expression.

Bash Script File

</>
Copy
#!/bin/bash
 
a=1
b=1
a_max=7
b_max=5
 
# and opertor used to form a compund expression
while [[ $a -lt $a_max+1 || $b -lt $b_max+1 ]]; do
   echo "$a"
   let a++
   let b++
done

Output

1
2
3
4
5
6
7

The loop continues while either comparison remains true. Since a_max is 7 and b_max is 5, the second comparison becomes false first, but the first comparison remains true until a passes its limit.

Note that the comment inside the original example says “and operator,” but the expression itself uses ||, which is the Bash OR operator.

Difference Between Bash || and && Operators

|| and && both combine conditions or commands, but they make different decisions about the right-hand operand.

OperatorMeaningWhen the right side is evaluated
||Logical ORWhen the left side fails
&&Logical ANDWhen the left side succeeds

For conditions, use OR when either condition is enough. Use AND when all required conditions must succeed.

Bash || Compared with -o in Test Expressions

You may encounter older shell code that uses -o for OR inside a test expression. For Bash scripts, separate [ ... ] tests with ||, or use || directly inside [[ ... ]]. These forms make the logical structure clearer.

</>
Copy
if [ "$name" = "admin" ] || [ "$name" = "root" ]; then
    echo "Privileged account name"
fi

if [[ "$name" == "admin" || "$name" == "root" ]]; then
    echo "Privileged account name"
fi

Bash OR Operator: Common Mistakes

  • Do not confuse || with &&. OR needs only one successful operand; AND requires both.
  • When using separate [ ... ] tests, write each test as a complete command: [ condition_1 ] || [ condition_2 ].
  • Quote variable expansions that may contain spaces or be empty, especially when using [ ... ].
  • Remember that command1 || command2 is based on command exit statuses, not text values such as the words true and false.
  • Do not assume the right side of || always runs. Bash skips it whenever the left side succeeds.

Bash OR Operator: Key Points

  • || is the Bash logical OR operator.
  • An OR condition succeeds when either operand succeeds.
  • With commands, the right-hand command runs only when the left-hand command fails.
  • || can combine separate [ ... ] tests or conditions inside [[ ... ]].
  • Bash uses short-circuit evaluation, so the second operand may not be evaluated.

In this Bash Tutorial, we learned how the Bash OR operator works with conditional expressions, command exit statuses, IF statements, file tests, and while loops.

Bash OR Operator Editorial QA Checklist

  • Verify that every reference to || identifies it as OR rather than AND.
  • Check that the OR truth table is false only for the false/false combination.
  • Confirm that examples using [ ... ] contain two complete test commands around ||.
  • Confirm that examples using [[ ... ]] place || correctly inside the compound conditional expression.
  • Verify that command-chaining explanations describe Bash exit status correctly: zero is success and non-zero is failure.
  • Check that short-circuit examples explain that the right side of || is skipped after a successful left side.
  • Ensure output shown for every newly added Bash example matches the script behavior.