Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question

closed as unclear what you're asking by slm, jasonwryan, Anthon, Mat, Joseph R. Aug 31 '13 at 8:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Neither of these "crash" for me. What version of bash? –  slm Aug 31 '13 at 2: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 2:05
1  
This was asked before and well answered: unix.stackexchange.com/questions/88850/… –  Drav Sloan Aug 31 '13 at 5: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. –  James Andino Sep 1 '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 Sep 30 '13 at 20:24

1 Answer 1

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
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.