Unix’s mysterious && and ||

By Sandra Henry-Stocker

The Unix shell’s && and || operators provide some very useful functionality, but they can be a bit mysterious, especially considering the number of different options for how they are used. The most common use of these Boolean operators is in the construction of multi-conditional tests – when you want two or more conditions to be true (or any in a set of operators to be true) before some command is run. The && serves as a logical AND (requiring all conditions to be true) operation while the || provides a logical OR (requiring only one to be true).

Combining tests

In the script below, we’re using && to combine two very simple conditions. We won’t get output unless both conditions are true. This particular script runs through the tests twice, but only to demonstrate the two “flavors” of the brackets that can be used. Note that && doesn’t work inside square brackets unless they’re doubled.

To read this article in full or to leave a comment, please click here

From: Network World