Confusing use of && and || operators

I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I’m studying for an exam), and I’ve become a bit confused about the && and the || operators. I’ve read where they can be used in statements such as:

if [ test1 ] && [ test2 ]; then
     echo "both tests are true"
elif [ test1 ] || [ test2 ]; then
     echo "one test is true"
fi

However, this script shows single line statements such as:

[ -z "$SMQUEUE" ] && SMQUEUE="QUEUE"
[ -f /usr/sbin/sendmail ] || exit 0

These seem to be using the && and || operators to elicit responses based on tests, but I haven’t been able to dig up documenation regarding this particular use of these operators. Can anyone explain what these do in this particular context?

Asked By: josh-cain

||

The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false).

You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.

Examples:

$ false && echo howdy!

$ true && echo howdy!
howdy!
$ true || echo howdy!

$ false || echo howdy!
howdy!

Extra notes:

If you do which [, you might see that [ actually does point to a program! It’s usually not actually the one that runs in scripts, though; run type [ to see what actually gets run. If you wan to try using the program, just give the full path like so: /bin/[ 1 = 1.

Answered By: Shawn J. Goff

to expand on @Shawn-j-Goff’s answer from above, && is a logical AND, and || is a logical OR.

See this part of the Advanced Bash Scripting Guide. Some of the contents from the link for user reference as below.

&&
AND

if [ $condition1 ] && [ $condition2 ]
#  Same as:  if [ $condition1 -a $condition2 ]
#  Returns true if both condition1 and condition2 hold true...

if [[ $condition1 && $condition2 ]]    # Also works.
#  Note that && operator not permitted inside brackets
#+ of [ ... ] construct.

||
OR

if [ $condition1 ] || [ $condition2 ]
# Same as:  if [ $condition1 -o $condition2 ]
# Returns true if either condition1 or condition2 holds true...

if [[ $condition1 || $condition2 ]]    # Also works.
#  Note that || operator not permitted inside brackets
#+ of a [ ... ] construct.
Answered By: Tim Kennedy

There is a notion of “short cutting”.

When (expr1 && expr2) is evaluated – expr2 is only evaluated if exp1 evaluates to “true”. This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to “false” expr2 is NOT evalued (short cut) because (expr1 && expr2) is already “flase”.

Try the following – assume file F1 exists & file F2 does not exist:

( [ -s F1 ] && echo "File Exists" )  # will print "File Exists" - no short cut
( [ -s F2 ] && echo "File Exists" )  # will NOT print "File Exists" - short cut

Similarly for || (or) – but short cutting is reversed.

Answered By: Larry

From my experience I use the && and || to reduce an if statement to a single line.

Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell:

if [ -f /root/Sample.txt ]
then
    echo "file found"
else
    echo "file not found"
fi

These 6 lines can be reduced to a single line:

[[ -f /root/Sample.txt ]] && echo "file found" || echo "file not found"

When running a few iterations to set variables or to create files etc., life is easier and the script looks slicker using the single line if function, it’s only drawback is that it becomes a bit more difficult to implement multiple commands from a single iteration however you can make use of functions.

Answered By: syme89

Here’s my cheat sheet:

  • “A ; B” Run A and then B, regardless of success of A
  • “A && B” Run B if A succeeded
  • “A || B” Run B if A failed
  • “A &” Run A in background.
Answered By: user177073

The examples in the answer by Shawn J. Goff are correct, but the explanation is the other way around. Please check:

The right side of && will only be evaluated if the exit status of the left side is NONZERO. || is the opposite: it will evaluate the right side only if the left side exit status is ZERO.

Answered By: Try2Help
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.