Bird
0
0

Why does the command cmd1 && cmd2 || cmd3 sometimes run cmd3 even if cmd1 succeeds?

hard📝 Conceptual Q10 of 15
Linux CLI - Pipes and Redirection
Why does the command cmd1 && cmd2 || cmd3 sometimes run cmd3 even if cmd1 succeeds?
ABecause || has higher precedence than &&
BBecause && and || have equal precedence and left associativity
CBecause cmd2's exit code affects || execution
DBecause cmd3 always runs after cmd2
Step-by-Step Solution
Solution:
  1. Step 1: Understand operator precedence

    In shell, && and || have equal precedence and are left associative, so evaluated left to right.
  2. Step 2: Analyze command behavior

    cmd1 && cmd2 runs first; if cmd2 fails, || cmd3 runs. If cmd2 succeeds, cmd3 does not run.
  3. Final Answer:

    Because && and || have equal precedence and left associativity, cmd3 runs only if cmd2 fails. -> Option B
  4. Quick Check:

    Equal precedence and left associativity explain chaining [OK]
Quick Trick: && and || have equal precedence, evaluated left to right [OK]
Common Mistakes:
  • Assuming || has higher precedence
  • Thinking cmd3 always runs
  • Ignoring left associativity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes