How to Use Logical OR & AND in Bash Script

In Bash scripting, logical 'AND' and 'OR ' operators are used to combine multiple conditions in conditional statements or commands. These operators are crucial for controlling the flow of a script based on the success or failure of certain conditions.

Logical 'AND' Operator (&&)

The 'AND' operator is represented by &&. It is used to execute a command or set of commands only if the preceding command or condition succeeds (returns a zero exit status).

Syntax:
condition1 && condition2 && ... conditionN
Example:
#!/bin/bash echo "Starting the script..." # Check if a file exists and if it is readable if [ -e "example.txt" ] && [ -r "example.txt" ]; then echo "File 'example.txt' exists and is readable." else echo "File 'example.txt' either doesn't exist or is not readable." fi echo "Script completed."

In this example, the second command (echo "File 'example.txt' exists and is readable.") will only be executed if both conditions inside the if statement are true.

Logical 'OR' Operator (||)

The 'OR' operator is represented by . It is used to execute a command or set of commands if the preceding command or condition fails (returns a non-zero exit status).

Syntax:
condition1 || condition2 || ... conditionN
Example:
#!/bin/bash echo "Starting the script..." # Check if a file exists or if it is writable if [ -e "example.txt" ] || [ -w "example.txt" ]; then echo "File 'example.txt' exists or is writable." else echo "File 'example.txt' doesn't exist and is not writable." fi echo "Script completed."

In this example, the second command (echo "File 'example.txt' exists or is writable.") will be executed if at least one of the conditions inside the if statement is true.

Combining 'AND' and 'OR' operators

You can also combine both 'AND' and 'OR' operators in more complex conditions to achieve the desired logic flow.

Example:
#!/bin/bash echo "Starting the script..." # Check if a file exists and is readable, or if it is writable if [ -e "example.txt" ] && [ -r "example.txt" ] || [ -w "example.txt" ]; then echo "File 'example.txt' exists and is readable, or it is writable." else echo "File 'example.txt' doesn't meet the specified conditions." fi echo "Script completed."

In this example, the second command (echo "File 'example.txt' exists and is readable, or it is writable.") will be executed if the file exists and is readable or if it is writable.

Ponits to Remember:

  1. The order of evaluation: For both operators, the leftmost condition is evaluated first. Subsequent conditions are only evaluated if necessary. In the AND operator, further evaluation stops as soon as a condition is false. In the OR operator, evaluation continues until a true condition is found or all conditions are evaluated.
  2. Using parentheses: You can use parentheses to group conditions and control the evaluation order more precisely.
  3. Return values: Commands within these operators also return values. If their exit code is 0 (true), the operator treats it as true, and vice versa.

Additional Notes:

  1. The -a and -o notations are less common but alternative ways to represent the AND and OR operators.
  2. While these operators are powerful, consider using [[ ... ]] constructs for more readable and flexible conditional expressions in modern Bash scripting.

Conclusion

The logical 'AND' operator (&&) is used to execute commands only if the preceding command or condition succeeds, while the 'OR' operator (||) is used to execute commands if the preceding command or condition fails. These operators allow for conditional branching based on the success or failure of specific conditions in scripts.