📖 Scenario: You are managing files in a Linux terminal. You want to run multiple commands in a sequence, but sometimes you want the next command to run only if the previous one succeeded or failed. This is common when automating tasks like backups or installations.
🎯 Goal: Learn how to use command chaining operators &&, ||, and ; to control the flow of commands in the Linux shell.
📋 What You'll Learn
Create three simple commands using echo with specific messages
Use && to run a command only if the previous command succeeds
Use || to run a command only if the previous command fails
Use ; to run commands sequentially regardless of success or failure
Print the final output to show the effect of chaining
💡 Why This Matters
🌍 Real World
System administrators and developers often chain commands to automate tasks like backups, installations, and cleanups efficiently.
💼 Career
Knowing command chaining is essential for writing shell scripts and managing Linux servers, a common skill in IT and DevOps roles.
Progress0 / 4 steps
1
Create three echo commands
Write three separate echo commands that print exactly these messages: First command, Second command, and Third command. Write each command on its own line.
Linux CLI
Hint
Use echo "message" to print text in the terminal.
2
Chain commands with &&
Use the && operator to chain the three echo commands so that each command runs only if the previous command succeeds. Write all three commands on a single line separated by &&.
Linux CLI
Hint
Use command1 && command2 to run command2 only if command1 succeeds.
3
Chain commands with ||
Write a command line that tries to list a non-existent directory with ls /no_such_dir, then uses || to print Directory not found only if the ls command fails.
Linux CLI
Hint
Use command1 || command2 to run command2 only if command1 fails.
4
Chain commands with ;
Write a single line that runs these three echo commands separated by ; so that all commands run one after another regardless of success or failure.
Linux CLI
Hint
Use command1; command2 to run commands one after another no matter what.
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
Step 1: Understand the meaning of &&
The && operator runs the next command only if the previous command returns success (exit status 0).
Step 2: Compare with other operators
|| runs next command only if previous failed, and ; runs commands regardless of success.
Final Answer:
Runs the next command only if the previous command succeeded -> Option A
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
Step 1: Identify the operator that runs commands regardless
The semicolon ; runs commands one after another no matter if the first succeeds or fails.
Step 2: Check other options
&& runs next only if first succeeds, || runs next only if first fails, & runs command in background.
Final Answer:
cmd1 ; cmd2 -> Option C
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
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.
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.
Final Answer:
File missing -> Option B
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
Step 1: Analyze the chaining logic
Operators || and && have equal precedence and associate left-to-right: parsed as (mkdir newdir || echo "Created") && cd newdir.
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.
Final Answer:
The echo message is incorrect -> Option A
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
Step 1: Understand the requirement
Run backup.sh only if prepare.sh succeeds; if prepare.sh fails, run 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.
Final Answer:
prepare.sh && backup.sh || cleanup.sh -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.