0
A && B ; C
  • A returns an exit status of zero.

  • B returns a non-zero exit status.

  • C, if performed, returns an exit status of zero.

Then, which of the following two will happen (A is taken for granted to be performed in either case):

  1. Bash will perform B and C

  2. Bash will perform B but not C

As an extension to this question, how would we modify the chain to allow for the behaviour of the alternative (1 or 2)?

Edit:

I'm not asking about the meaning of && versus ; .

I know A and B gets executed regardless. The remaining scenario is either C are ignored, because B returned non-zero exit status, or B is skipped over and C is re-evaluated on its own merit.

ptrcao
  • 5,635
  • no, if set -e is in effect or your computer is a hit by a meteorite just after running the B command. –  Aug 17 '19 at 17:46
  • @muru I don't think you read my question; I've already seen that other question you linked - it's not a duplicate. The scope and the question is completely different – ptrcao Aug 17 '19 at 17:48
  • 1
    So you do understand the difference between && and ;? – muru Aug 17 '19 at 17:49
  • @muru Absolutely. That's not my question. My question is about the grouping of the logic. – ptrcao Aug 17 '19 at 17:49
  • What grouping? You haven't applied any grouping via braces or parentheses. – muru Aug 17 '19 at 17:52
  • Sorry, but I assume that you have not understood your own question or the basics, because it is executed A, B and C and it is neither 1 nor 2. – Cyrus Aug 17 '19 at 17:55
  • @Cyrus Not true. So what I'm sayin is I know A gets executed regardless. The remaining scenario is either both B and C are ignored, because B returned non-zero exit status, or B is skipped over and C is re-evaluated on its own merit. – ptrcao Aug 17 '19 at 17:58
  • 2
    Your Q says nothing about any "grouping". If you're confused about operator precendence, then && has higher precedence than ;. A && B; C is { A && B; }; C not A && { B; C; } (all this is simplified, since ; is not actually an operator, but a separator, and { A; } is not exactly equivalent to A in the shell. –  Aug 17 '19 at 18:00
  • @ptrcao: The exit status of B is irrelevant and C is always executed. – Cyrus Aug 17 '19 at 18:01
  • @mosvy has interpreted my question correctly. I'm sorry if my wording was unclear but essentially mosvy's reading is correct. Welcome to leave as answer and I will accept – ptrcao Aug 17 '19 at 18:03
  • @jsotola You are right; I have fixed the question so that doesn't distract from the main question I was asking. – ptrcao Aug 17 '19 at 18:29

1 Answers1

4
A && B ; C

A returns an exit status of zero.

In this scenario B will be run next, and regardless of the exit values of A and B, C will run.

That sequence groups like (A && B) ; C, not as A && (B ; C). You could add a grouping with braces or parenthesis (subshell) if you want to.

The remaining scenario is either both B and C are ignored, because B returned non-zero exit status,

This doesn't make much sense: B can't get ignored based on the exit status of B itself.

or B is skipped over and C is re-evaluated on its own merit.

B gets skipped if and only if A returns a falsy (nonzero) status, not if it returns a truthy (zero) status.

Note that the shell has an inverted idea of the truth values of numbers compared to e.g. the C language where zero is false.

If A returns a falsy (nonzero) status, then B doesn't run, but C still does.

ilkkachu
  • 138,973