Americas

  • United States
sandra_henrystocker
Unix Dweeb

The logic of && and || on Linux

How-To
May 02, 20244 mins
Linux

These AND and OR equivalents can be used in scripts to determine next actions.

Person using keyboard and mouse
Credit: NDAB Creativity / Shutterstock

We’ve all probably seen the && and || operators in use from time to time. These AND and OR equivalents are often used in scripts to make decisions about what happens next. In this post, we’ll take a look at how they work at a very basic level and some ways they are often used in scripts.

Using ||

To begin, the || (OR) operator aligns itself with the use of the word “or” in common day English. However, it makes a clear distinction. If the command on the left side of the || is successfully run, the command on the right is not run. Run the command below and you’ll never see it respond “goodbye”.

$ echo hello || echo goodbye
hello

On the other hand, if the command on the left of the || fails, the one on the right side will be run.

$ cat nosuchfile || echo oops!
cat: nosuchfile: No such file or directory
oops!

You could get the same response (without the error) with an if command like this one:

$ if [ ! -f nosuchfile ]; then
>   echo oops!
> fi
oops!

If the commands on both sides of the || operator fail, you just see more than one error message.

$ cat nosuchfile || cal nosuchmonth
cat: nosuchfile: No such file or directory
cal: failed to parse timestamp or unknown month name: nosuchmonth

Using &&

The && operator uses different logic. It will run the commands on both sides of the && as long as the one on the left completes successfully. This is the AND logic – like “this AND that”.

$ echo hello && echo goodbye
hello
goodbye

If, on the other hand, the command on the left side fails, the command on the right side is not run. The AND logic (run both commands) simply gives up. The command line just stops with the initial failure. Notice that the “no surprise there” string below is not displayed.

$ cat nosuchfile && echo no surprise there
cat: nosuchfile: No such file or directory

Combining || and &&

There’s no reason that you can’t use both || and && operators in a single command. Doing so just makes the parsing a bit trickier. Run a command like this and watch what happens:

$ echo this || echo that && echo the other
this
the other

The first echo command runs successfully, so the second isn’t run (normal OR behavior). The left side of the AND (&&) was still run successfully, so the right side runs as well.

In the next example, we test whether the /etc/shadow file (the file which holds password encryptions etc.) can be opened for reading. Note the result. The file isn’t opened because the command is not being run with superuser privilege. Note also that error output is being sent to /dev/null so as not to be displayed.

$ cat /etc/shadow 2>/dev/null && echo "file opened" || echo "file failed to open"
file failed to open

The failure of opening the /etc/shadow file means the command following the && (AND) is not run. Yet the command following the || (OR) operator is run because, well, that’s the way || works.

Run the command with sudo and you’ll get a different response. Note, though, that I am sending the cat command’s output to /dev/null to avoid stuffing all those lines of output into this post!

$ sudo cat /etc/shadow >/dev/null && echo "file successfully opened" || echo "file failed to open"
file successfully opened

Wrap-up

The || (OR) and && (AND) operators can be very useful, but it’s always a good idea to test your logic to make sure it follows the rules you are expecting before entrusting them to a script.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.