0
0
Linux CLIscripting~5 mins

Command chaining (&&, ||, ;) in Linux CLI - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the && operator do in command chaining?
It runs the second command only if the first command succeeds (returns exit status 0). Think of it as "and then if successful."
Click to reveal answer
beginner
What happens when you use the || operator between two commands?
The second command runs only if the first command fails (returns non-zero exit status). It's like saying "or else run this."
Click to reveal answer
beginner
How does the ; operator work in command chaining?
It runs commands sequentially, one after another, regardless of whether the previous command succeeded or failed.
Click to reveal answer
intermediate
Example: What will happen with mkdir test && cd test if the directory already exists?
The mkdir test will fail because the directory exists, so cd test will NOT run because of the && operator.
Click to reveal answer
intermediate
Why might you use command1 || command2 in a script?
To try command1 and if it fails, automatically run command2 as a fallback or error handler.
Click to reveal answer
What does cmd1 && cmd2 do?
ARuns cmd2 first, then cmd1
BRuns cmd2 only if cmd1 fails
CRuns cmd2 only if cmd1 succeeds
DRuns cmd1 and cmd2 regardless of success
What happens with cmd1 || cmd2?
ARuns cmd2 only if cmd1 succeeds
BRuns cmd2 only if cmd1 fails
CRuns both commands always
DRuns cmd1 twice
Which operator runs commands one after another no matter what?
A;
B||
C&&
D|
If you want to run cmd2 only when cmd1 fails, which do you use?
Acmd1 | cmd2
Bcmd1 && cmd2
Ccmd1 ; cmd2
Dcmd1 || cmd2
What will mkdir newdir; cd newdir do?
ARun mkdir, then cd regardless of mkdir success
BRun cd only if mkdir succeeds
CRun cd only if mkdir fails
DRun neither command
Explain how the &&, ||, and ; operators differ in chaining commands.
Think about success and failure conditions for each operator.
You got /3 concepts.
    Describe a real-life situation where you would use command1 || command2 in a script.
    Imagine trying something and doing something else if it doesn't work.
    You got /3 concepts.