Logical operators are used to combine multiple conditions in a script, allowing for more complex decision-making. The two most commonly used logical operators in shell scripting are Logical OR (||) and Logical AND (&&).
These operators are used in conditional statements like if, while, and until, as well as in command chaining. Command chaining is a technique where multiple commands are executed in a sequence, based on the success or failure of previous commands.
- The Logical OR operator (||) is used to execute the command following it only if the previous command fails (returns a non-zero exit status).
- The Logical AND operator (&&) is used to execute the command following it only if the previous command is successful (returns a zero exit status).
1. Using Logical
OR (||) in Shell Scripts
Logical OR in bash script is used with operator -o. Write a small shell script that will show how to use logical OR ( || ) operator between two conditions.
This shell script prompts the user to input two numeric values. It then checks if either the first value is less than or equal to 10, or the second value is greater than 20. If at least one of these conditions is met, the script outputs “At least one condition is true.” Otherwise, it outputs “Both conditions are failed.”
2. Using Logical
AND (&&) in Shell Scripts
Logical AND in bash script is used with operator -a. Below shell script will show you to how to use logical AND ( && ) between two conditions.
This shell script prompts the user to input two numeric values. It then checks if the first value is less than or equal to 10 and the second value is greater than 20. If both conditions are met, the script outputs “Both conditions are true.” Otherwise, it outputs “At least one condition is false.”
3. Using Multiple Logical OR & AND
Now, use the multiple logical operators in a single statement. The below example will help you to understand how to use multiple logical operators in a single statement.
This shell script allows a user to input a number and then checks if the entered number lies within either the range of 10-20 or 100-200. The script then outputs a message to the user indicating whether the input number falls within one of these specified ranges or not.
4. A Practical Example
In this practical example, we’ll create a shell script that checks if a server is reachable and if a specific directory exists on the local machine. If both conditions are met, the script will create a backup of the directory and transfer it to the remote server using scp. If any of these conditions are not met, it will print an appropriate error message.
Create a file called backup_and_transfer.sh and add the following content:
This script demonstrates the use of Logical AND (&&) and Logical OR (||) operators to chain commands, create backup files, and transfer them to a remote server, while handling errors and providing useful feedback.
Mastering the Logical OR (||) and Logical AND (&&) operators in shell scripting enables you to create efficient and effective scripts for managing systems and automating tasks. By understanding their usage and combining them as needed, you can create complex decision trees and streamline your shell scripts. Remember to follow best practices, such as using parentheses to group conditions and leveraging short-circuit evaluation, to optimize your scripts and ensure they work as intended. With practice and a thorough understanding of these logical operators, you’ll be well on your way to becoming a shell scripting expert.
Even though there is a “modern approach” some of us might be trying to read old scripts. There are plenty of scripts out there that use -o and -a. Do you think, suddenly, they will all stop working?
Using “-o” and “-a” is deprecated. The modern approach is “||” and “&&” with enclosing brackets .
Also, your example for using multiple and/or in bash does not work and gets a syntax error (you can’t use parenthesis like that inside brackets; did you test it?). You CAN uses parenthesis if using “||” or “&&” outside of brackets.
Hi Mark, I have updated the tutorial with a modern approach.
Thank you for your valuable suggestions.
Thank you so much! I was so lost, could not find this anywhere. 🙂
Thank You. 🙂