Bird
Raised Fist0
Linux CLIscripting~20 mins

Command chaining (&&, ||, ;) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Command Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
πŸ’» Command Output
intermediate
2:00remaining
What is the output of this chained command?
Consider the following command run in a Linux shell:

echo Hello && echo World || echo Fail

What will be printed?
Linux CLI
echo Hello && echo World || echo Fail
AHello\nWorld
BHello\nFail
CWorld
DFail
Attempts:
2 left
πŸ’‘ Hint
Remember that && runs the next command only if the previous succeeds, and || runs the next command only if the previous fails.
πŸ’» Command Output
intermediate
2:00remaining
What happens with semicolon chaining?
What is the output of this command?

echo Start; false; echo End

Note: 'false' is a command that always fails.
Linux CLI
echo Start; false; echo End
AEnd
BStart
CStart\nEnd
DStart\nfalse\nEnd
Attempts:
2 left
πŸ’‘ Hint
Semicolon runs all commands regardless of success or failure.
πŸ’» Command Output
advanced
2:00remaining
What is the output of this mixed chaining?
What will this command print?

false && echo No || echo Yes; echo Done
Linux CLI
false && echo No || echo Yes; echo Done
AYes\nDone
BNo\nDone
CNo
DDone
Attempts:
2 left
πŸ’‘ Hint
&& runs next only if previous succeeds; || runs next only if previous fails; semicolon always runs next.
πŸ’» Command Output
advanced
2:00remaining
What error or output occurs here?
What happens when running this command?

mkdir test_dir && cd test_dir || echo Failed
Linux CLI
mkdir test_dir && cd test_dir || echo Failed
Acd: no such file or directory: test_dir
BNo output, current directory changes to test_dir
Cmkdir: cannot create directory β€˜test_dir’: File exists
DFailed
Attempts:
2 left
πŸ’‘ Hint
If mkdir succeeds and cd succeeds, || echo Failed does not run.
πŸ’» Command Output
expert
3:00remaining
What is the output of this complex chain?
Analyze this command:

echo Start && false || echo Middle && echo End

What will be printed?
Linux CLI
echo Start && false || echo Middle && echo End
AStart\nMiddle
BMiddle\nEnd
CStart\nEnd
DStart\nMiddle\nEnd
Attempts:
2 left
πŸ’‘ Hint
Remember operator precedence: && has higher precedence than ||, so group accordingly.

Practice

(1/5)
1. What does the command chaining operator && do in Linux shell?
easy
A. Runs the next command only if the previous command succeeded
B. Runs the next command only if the previous command failed
C. Runs all commands regardless of previous command success
D. Runs the next command in the background

Solution

  1. Step 1: Understand the meaning of &&

    The && operator runs the next command only if the previous command returns success (exit status 0).
  2. Step 2: Compare with other operators

    || runs next command only if previous failed, and ; runs commands regardless of success.
  3. Final Answer:

    Runs the next command only if the previous command succeeded -> Option A
  4. Quick Check:

    && means run next if success [OK]
Hint: Remember: && means next runs only on success [OK]
Common Mistakes:
  • Confusing && with ||
  • Thinking && runs commands regardless
  • Assuming && runs commands in background
2. Which of the following is the correct syntax to run cmd1 and then cmd2 regardless of cmd1's success or failure?
easy
A. cmd1 && cmd2
B. cmd1 || cmd2
C. cmd1 ; cmd2
D. cmd1 & cmd2

Solution

  1. Step 1: Identify the operator that runs commands regardless

    The semicolon ; runs commands one after another no matter if the first succeeds or fails.
  2. Step 2: Check other options

    && runs next only if first succeeds, || runs next only if first fails, & runs command in background.
  3. Final Answer:

    cmd1 ; cmd2 -> Option C
  4. Quick Check:

    ; runs commands regardless [OK]
Hint: Use ; to run commands one after another always [OK]
Common Mistakes:
  • Using && or || when unconditional run is needed
  • Confusing & (background) with ;
  • Missing spaces around operators
3. What will be the output of the following commands if file.txt does not exist?
cat file.txt && echo "File exists" || echo "File missing"
medium
A. File exists
B. File missing
C. No output
D. cat: file.txt: No such file or directory

Solution

  1. Step 1: Understand command chaining behavior

    cat file.txt fails because file does not exist, so && echo "File exists" is skipped, and || echo "File missing" runs.
  2. Step 2: Predict output

    The error message from cat is printed to stderr (not shown here), but the command output from chaining is File missing.
  3. Final Answer:

    File missing -> Option B
  4. Quick Check:

    Failed cat triggers || echo [OK]
Hint: Remember: || runs next if previous fails [OK]
Common Mistakes:
  • Expecting 'File exists' output
  • Ignoring that cat prints error to stderr
  • Thinking no output appears
4. Identify the error in this command chaining:
mkdir newdir || echo "Created" && cd newdir
medium
A. The echo message is incorrect
B. mkdir should be followed by ; not ||
C. The chaining logic runs cd only if echo runs
D. No error, command works as intended

Solution

  1. Step 1: Analyze the chaining logic

    Operators || and && have equal precedence and associate left-to-right: parsed as (mkdir newdir || echo "Created") && cd newdir.
  2. Step 2: Trace execution

    mkdir success: skip echo, success && cd runs. mkdir fail: run echo "Created" (success) && cd runs. Error: echo "Created" runs only on failure (dir exists), wrong message.
  3. Final Answer:

    The echo message is incorrect -> Option A
  4. Quick Check:

    || echo "Created" runs on mkdir fail [OK]
Hint: || triggers echo only on failure, check message [OK]
Common Mistakes:
  • Misparsing as right-associative (mkdir || (echo && cd))
  • Thinking cd skips on mkdir success
  • Overlooking echo on mkdir failure
5. You want to run backup.sh only if prepare.sh succeeds, but if prepare.sh fails, you want to run cleanup.sh. Which command chaining achieves this?
hard
A. prepare.sh || cleanup.sh && backup.sh
B. prepare.sh && (backup.sh || cleanup.sh)
C. prepare.sh ; backup.sh ; cleanup.sh
D. prepare.sh && backup.sh || cleanup.sh

Solution

  1. Step 1: Understand the requirement

    Run backup.sh only if prepare.sh succeeds; if prepare.sh fails, run cleanup.sh.
  2. Step 2: Analyze prepare.sh && backup.sh || cleanup.sh

    prepare.sh && backup.sh || cleanup.sh means: if prepare.sh succeeds, run backup.sh. If either prepare.sh or backup.sh fails, run cleanup.sh. This matches the requirement.
  3. Final Answer:

    prepare.sh && backup.sh || cleanup.sh -> Option D
  4. Quick Check:

    Run backup if prepare succeeds, else cleanup [OK]
Hint: Use && then || to handle success and failure paths [OK]
Common Mistakes:
  • Running cleanup even if prepare succeeds
  • Running backup even if prepare fails
  • Using ; which ignores success/failure
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard