0
0
Linux CLIscripting~20 mins

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

Choose your learning style9 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.