Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell : logical ANDs and ORs instead of if-else

I was wondering why

i=10
  if [ $i -lt 5 ]; then
    echo "$i < 5"
elif [ $i -gt 5 ]; then
    echo "$i > 5"
elif [ $i -eq 5 ]; then
    echo "$i = 5"
fi

Outputs proper result:

10 > 5

Whereas

i=10
     [ $i -lt 5 ] && {
    echo "$i < 5"
} || [ $i -gt 5 ] && {
    echo "$i > 5"
} || [ $i -eq 5 ] && {
    echo "$i = 5"
}

behaves an unusual way:

10 > 5
10 = 5

In my opinion, as the interpreter seeks for 1s, it should work like this:

0 && {} || 1 && {} || 0 && {}

0 so the 0 && {} is definitely 0; skip {}

1 means that {} must be checked to define the value of whole 1 && {}

So that the result is 1, but the only {} is executed stays after 1. However, this all does work as it should when I put ! { instead of {s.

i=10
     [ $i -lt 5 ] && ! {
    echo "$i < 5"
} || [ $i -gt 5 ] && ! {
    echo "$i > 5"
} || [ $i -eq 5 ] && ! {
    echo "$i = 5"
}

WHY?! I thought it seeks for 1s so since it finds a 0 in a && it doesn't look at other expressions in the chain!

like image 229
theoden8 Avatar asked Jan 09 '23 00:01

theoden8


1 Answers

The {...} does not make a difference, so what you have is equivalent to this:

i=10
[ $i -lt 5 ] && 
echo "$i < 5" ||
[ $i -gt 5 ] &&
echo "$i > 5" || 
[ $i -eq 5 ] &&
echo "$i = 5"

And the way this works is:

  1. [ $i -lt 5 ]: This is false (returns failure), so it jumps to the next ||, which has [ $i -gt 5 ] following it.

  2. [ $i -gt 5 ]: This is true (returns success), so it jumps to the next &&, which has echo "$i > 5" following it.

  3. echo "$i > 5": This returns success, so it jumps to the next &&, which has echo "$i = 5" following it.

  4. echo "$i = 5": This returns success, so it jumps to... wait no, there's a newline. We're done.

&& and || are called short-circuit operators.

EDIT: To stress the point further,

A && B || C

is NOT the same as

if A; then
    B
else
    C
fi

It's equivalent to

if A; then
    if ! B; then
        C
    fi
else
    C
fi
like image 116
geirha Avatar answered Jan 16 '23 02:01

geirha