0

This question already has an answer here:

Note: A B C are commands.....

Please tell me what the step process would be of these commands, so I can better understand the language..

A || B ; C = If A fails then run B then C?
A ; B || C = Run A then B then C if (B fails... or is it if A fails?)?

What I am really after is something like this (but would like to understand the steps above):

If A fails, then run B, but if A succeeds, skip B then run C,d,e,etc (How can I do this with "||" ";" and/or "&&"?)

I found information from this link, but it only shows the steps of 2 commands, not 3.... https://askubuntu.com/questions/334994/which-one-is-better-using-or-to-execute-multiple-commands-in-one-line

Reason why another link does not answer this question:

So, next time if you need to chain 4 commands you'll ask a new question because the answers here only show how 3 commands work ?– don_crissti26 mins ago

No sir, because I'll understand the linguistic next step for the future :). Mostly because of this....: I was unclear if the command and switch(?) " || " in sequential order (command C in this case) was always looking at command (A) or if it was looking command (B). This is mostly because I was confused by someone saying this: A || B = Run B if A failed.. wasn't sure if the next command (C, D, etc etc) would look at A as well :) (duh is what it seems, but I don't know this language... so had to get clarity).

marked as duplicate by don_crissti, Stephen Kitt, Christopher, taliezin, jw013 Jun 5 '15 at 22:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @don_crissti It is very similiar except they only show how 2 commands work.. not 3 :) – R0tten Jun 5 '15 at 21:47
  • So, next time if you need to chain 4 commands you'll ask a new question because the answers here only show how 3 commands work ? – don_crissti Jun 5 '15 at 21:48
  • No sir, because I'll understand the linguistic next step for the future :). Mostly because of this....: I was unclear if the command and switch(?) " || " in sequential order (command C in this case) was always looking at command (A) or if it was looking command (B). This is mostly because I was confused by someone saying this: A || B = Run B if A failed.. wasn't sure if the next command (C, D, etc etc) would look at A as well :) (duh is what it seems, but I don't know this language... so had to get clarity). – R0tten Jun 5 '15 at 22:12
  • 1
    @R0tten && and || are left-associative binary operators with equal precedence. The ; is not an operator at all but merely a simple separator, like a newline, and as such has the lowest "precedence". That should be enough to clear up any confusions you have. – jw013 Jun 5 '15 at 23:03
2
A || B ; C

if A exits with a non-zero status, run B. C runs unconditionally

A ; B || C

run A. Then run B. If B exits with a non-zero status, run C


Tangientially, you'll sometimes see A && B || C. This is usually intended to be a shorthand for if A; then B; else C; fi. However, there is one major difference:

A && B || C
  • If A fails, run C
  • if A succeeds, then run B.
    • if B fails, run C
if A; then B; else C; fi
  • If A fails, run C
  • if A succeeds, then run B.
    • if B fails, C is NOT executed

A demo:

$ (echo A; exit 0) && (echo B; exit 1) || (echo C; exit 2); echo $?
A
B
C
2     # <== the exit status of C

$ if (echo A; exit 0); then (echo B; exit 1); else (echo C; exit 2); fi; echo $?
A
B
1     # <== the exit status of B
1

A simple test

(echo -n "hello" || echo -n "world") && echo "!"

produces "hello!"

Alternatively (making statement A false)

( false || echo -n "world") && echo "!"

produces "world!"

OR operators are lazy, thus if A is true in (A || B), B is never evaluated, as True OR True == True and True OR False == True

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