-3

Why does this not work as expected?

$ false &&   { echo ok;  echo ok; } && { echo notOK; }

and this not:

$ false &&   { echo ok;  echo ok; } || { echo notOK; }

I don't see it!

Tegra Detra
  • 5,016
  • 2
    Neither of these "crash" for me. What version of bash? – slm Aug 31 '13 at 02:03
  • When I run the 1st ex. it just exits (as I would expect), the 2nd one echos "notOK" (again as I would expect). – slm Aug 31 '13 at 02:05
  • 1
    This was asked before and well answered: http://unix.stackexchange.com/questions/88850/bash-logical-operators-precedence – Drav Sloan Aug 31 '13 at 05:09
  • the answer below disagrees with your guys comments, though this question is a duplicate; I do not know why I got stabbed up for asking it. – Tegra Detra Sep 01 '13 at 15:09
  • 1
    @JamesAndino You say that something that doesn't behave like you expect, without saying what you expected it to do instead or why you expected it to do that. Your question is incomprehensible, all we can do is take a stab in the dark and point you to some background on a related topic. – Gilles 'SO- stop being evil' Sep 30 '13 at 20:24

1 Answers1

10

This is how && and || work.

&& executes the right-hand-side if the left-hand-side finished with a "true" value (0).

|| executes the right-hand-side if the left-hand-side finished with a "false" value (! 0).

false && { echo ok; echo ok } || { echo notOK; }

Is processed this way:

  • false returns 1
  • && does not execute `{ echo ok; echo ok }' because the left-hand-side was not 0
  • || does execute { echo notOK; } because the left-hand-side was not 0
ash
  • 7,260