Linux Command Line Logical Operators AND (&&) OR (||) usage

Introduction:

In this article, you are going to learn how to use logical operators in the Linux command line.

Logical operators are fundamental tools that allow us to make decisions in our scripts and commands based on the outcomes of previously executed code. 

There are two types of logical operators available in Linux: the AND operator and the OR operator. 

These operators help us implement logical conditions in our command sequences, enhancing the functionality and efficiency of our scripts. 

Types of Logical Operators:

The AND operator (&&) in Linux is used to execute commands conditionally.  It works based on the exit status of the preceding command. 

In Linux, an exit status of 0 typically signifies success, while any non-zero exit status indicates some form of error or failure.

Syntax: command1 && command2

Usage: command2 is executed if and only if command1 succeeds (returns a zero exit status).

Example: $ mkdir new_directory && cd new_directory

Here, cd new_directory is executed only if mkdir new_directory is successful.


The OR operator (||) in Linux is used for conditional execution of commands. It operates based on the exit status of the preceding command. 

In Linux, if a command exits with a status of 0, it is deemed successful, and thus, the command following the OR operator is not executed. 

Conversely, a non-zero exit status, which indicates an error or some form of failure, triggers the execution of the command following the OR operator. 


Syntax: command1 || command2

Usage: command2 is executed if and only if command1 fails (returns a non-zero exit status).

Example: $ grep "text" file.txt || echo "text not found"

In this example, echo "text not found" is executed only if grep "text" file.txt fails to find the text in file.txt (meaning grep exits with a non-zero status).

Conclusion:

The AND operator is particularly useful in scripting and automation, where you want to proceed with subsequent actions only if the previous ones are successful.

It helps in error handling and maintaining the flow of command execution based on successful outcomes.

The OR operator (||) in Linux is very helpful, especially when you are writing scripts or doing automation tasks. If your first command fails, the OR operator helps by running a second command. This is great for handling mistakes or problems. 

It makes sure that even if one command doesn't do its job, your script can still do something else and keep going. This way, your script becomes more reliable and can handle different situations easily.

Useful Examples:

Example with One Operator (&& or ||):

systemctl is-active --quiet httpd && echo "HTTPD is running"


systemctl is-active --quiet httpd || echo "HTTPD is not running"

Example with Two Operators (&& or ||):

systemctl is-active --quiet httpd && echo "HTTPD is running" || echo "HTTPD is not running"


systemctl is-active --quiet httpd || echo "HTTPD not running" && systemctl start httpd

Example with Three Operators (Two && and One ||):

systemctl is-active --quiet httpd || (echo "HTTPD is not running" && echo "Starting HTTPD" && systemctl start httpd)


systemctl is-active --quiet httpd && echo "HTTPD is already running" || (echo "HTTPD is not running" && systemctl start httpd)