How to Use OR Operator in Bash Scripting?
Introduction
Before we start talking about how to use or operator in bash scripting, let's briefly understand-What is an OR Operator?
In Bash scripting, the ||
operator, also known as the OR operator, allows you to execute a command or set of commands only if the previous command fails or returns a non-zero exit status. This operator provides a convenient way to handle alternative actions based on the success or failure of previous commands.
The OR operators are used in the Bash script to check the conditions, and the results are returned as "true" or "false." Based on these outcomes (true or false), the subsequent execution of the tasks is then selected.
In this tutorial, we will explore how to effectively use the OR operator in Bash scripts. We will also address a few FAQs on how to use or operator in bash scripting.
OR Operator in Bash Script
The following syntax is used when the OR operator is used in a bash script:
Syntax
Condition 1 || condition 2 || Condition 3........Condition n
Enter conditions 1 and 2, then use the OR (||) operator to separate them.
When any of the provided conditions is met, the OR operator returns true; otherwise, it returns false. It resembles the truth table of OR that follows:
OR Truth Table
Condition 1 | Condition 2 | Result (Condition || Condition 2) |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Example 1: Using the OR Operator, determine the user's age
The following script will take the user's age and print it 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
As stated in the script:
- The "read" property displays the message to the user and records their age in the "age" variable.
- The "||" operator is used to separate the "if" conditions and determine whether the entered age is less than or equal to 17.
- If any of the conditions specified in the "then" section hold true, the "echo" command is carried out.
- If both conditions are false, the "else" section performs the "echo" command.
Exit after saving the script above.
Open the terminal and run the script file (script.sh):
bash script.sh
The age entered is "16," and the screen displays the message "You are Younger!"
Example 2: Print Number Using OR Operator in a Loop
The user's starting and ending numbers will be used to produce numbers in between using the script that follows:
#!/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 described as follows:
- The "read" property is utilized to accept two numbers (Stating and Ending) from the variables "start" and "end."
- The "while" loop is used to test whether the initial number is less than or equal to the ending number, with the two conditions being separated by the OR (||) operator.
- The starting value kept in the "$start" variable is printed by the "do" section.
- For the loop to continue, the line "((start=start+1))" increases the "$start" variable.
Save and exit the script above.
Run the "script.sh" script file from the terminal:
bash script.sh
The entered starting and ending numbers are 6 and 10. So, 6, 7, 8, 9, and 10 are the generated numbers.
Example 3: Check Condition Using “o” Operator
Instead of using the double pipe (||), which is also referred to as the OR operator in bash, the user can also use the "o" operator. Use it in the following script to determine whether a user-supplied number is even or divisible by three:
#!/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 described as follows:
- The "read" property displays the message while obtaining the user's input into the "num" variable.
- The "-o" operator, which determines if the entered number is even or divisible by 3, is used to separate the "if" conditions.
- If any of the provided conditions are true, the "then" portion executes the "echo" command.
- If both conditions are false, the "else" section runs the "echo" command.
Save and exit the script above.
Use the bash command to execute the script:
bash script.sh
The output is defined as
- First, the entered number was 5, and the message stated that the number was neither even nor divisible by three.
- Second, when the number 2 is entered, a message indicating the number is even or divisible by 3 is printed.
FAQs on Using the OR Operator in Bash Scripting
How does the OR operator work in Bash scripting?
When using the OR operator (||
), the command following ||
will be executed only if the command preceding it fails or returns a non-zero exit status.
When would I use the OR operator in Bash scripting?
You would use the OR operator when you want to perform alternative actions based on the success or failure of a command. It allows you to define a sequence of commands and decide which one to execute depending on the outcome of the previous commands.
Can I use the OR operator for multiple commands?
Yes, you can use the OR operator (||
) for multiple commands. If any of the preceding commands fail, the command following ||
will be executed.
Can I use the OR operator in an if statement?
Yes, you can use the OR operator in an if statement. By combining the OR operator with the if
statement, you can conditionally execute a block of code based on the success or failure of a command.
Can I use the OR operator to perform actions based on multiple conditions?
While the OR operator is primarily used to perform actions based on the success or failure of a single command, you can also use it to evaluate multiple conditions.
Is there an alternative to the OR operator in Bash scripting?
Yes, the OR operator can be alternated with the if-else
construct to achieve similar functionality. By checking the exit status of a command using if
and executing different actions based on the outcome, you can achieve similar results to the OR operator.
Can I negate the OR operator's behavior?
Yes, you can negate the behavior of the OR operator by using the !
operator before the entire expression. This will reverse the logic and execute the following command only if the preceding command succeeds.
Conclusion
The logical operator considered for checking the conditions in the Bash script is OR (||). It returns either "true" or "false" as the result. Any true conditions are returned with a "true" status, and any false conditions are with a "false" status. Depending on the need, the user may utilize the OR (||) operator in a script or a loop. In addition, the OR (||) operator results can also be obtained using the "-o" operator.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.