Command chaining (&&, ||, ;) in Linux CLI - Time & Space Complexity
We want to understand how the time to run chained commands changes as we add more commands.
How does the total work grow when commands are linked with &&, ||, or ; ?
Analyze the time complexity of the following command chaining example.
cmd1 && cmd2 || cmd3; cmd4
This runs cmd1, then cmd2 if cmd1 succeeds, else cmd3, and finally cmd4 regardless.
Here, each command runs once in sequence depending on previous results.
- Primary operation: Executing each command once.
- How many times: Number of commands in the chain (4 here).
As you add more commands chained, the total work grows roughly by the number of commands.
| Input Size (n) | Approx. Operations |
|---|---|
| 2 commands | 2 executions |
| 5 commands | 5 executions |
| 10 commands | 10 executions |
Pattern observation: The total steps grow linearly with the number of commands.
Time Complexity: O(n)
This means the total time grows directly with how many commands you chain.
[X] Wrong: "All commands always run no matter what, so time is always n times the longest command."
[OK] Correct: With && and ||, some commands may skip running depending on success or failure, so actual time can be less.
Knowing how command chaining affects execution helps you write efficient scripts and understand shell behavior clearly.
What if we replaced all && and || with ; ? How would the time complexity change?
Practice
&& do in Linux shell?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 AQuick Check:
&&means run next if success [OK]
- Confusing && with ||
- Thinking && runs commands regardless
- Assuming && runs commands in background
cmd1 and then cmd2 regardless of cmd1's success or failure?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 CQuick Check:
;runs commands regardless [OK]
- Using && or || when unconditional run is needed
- Confusing & (background) with ;
- Missing spaces around operators
file.txt does not exist?cat file.txt && echo "File exists" || echo "File missing"Solution
Step 1: Understand command chaining behavior
cat file.txtfails because file does not exist, so&& echo "File exists"is skipped, and|| echo "File missing"runs.Step 2: Predict output
The error message fromcatis printed to stderr (not shown here), but the command output from chaining isFile missing.Final Answer:
File missing -> Option BQuick Check:
Failed cat triggers || echo [OK]
- Expecting 'File exists' output
- Ignoring that cat prints error to stderr
- Thinking no output appears
mkdir newdir || echo "Created" && cd newdirSolution
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
mkdirsuccess: skip echo, success&& cdruns.mkdirfail: runecho "Created"(success)&& cdruns. Error:echo "Created"runs only on failure (dir exists), wrong message.Final Answer:
The echo message is incorrect -> Option AQuick Check:
|| echo "Created"runs on mkdir fail [OK]
- Misparsing as right-associative (mkdir || (echo && cd))
- Thinking cd skips on mkdir success
- Overlooking echo on mkdir failure
backup.sh only if prepare.sh succeeds, but if prepare.sh fails, you want to run cleanup.sh. Which command chaining achieves this?Solution
Step 1: Understand the requirement
Runbackup.shonly ifprepare.shsucceeds; ifprepare.shfails, runcleanup.sh.Step 2: Analyze prepare.sh && backup.sh || cleanup.sh
prepare.sh && backup.sh || cleanup.shmeans: ifprepare.shsucceeds, runbackup.sh. If eitherprepare.shorbackup.shfails, runcleanup.sh. This matches the requirement.Final Answer:
prepare.sh && backup.sh || cleanup.sh -> Option DQuick Check:
Run backup if prepare succeeds, else cleanup [OK]
- Running cleanup even if prepare succeeds
- Running backup even if prepare fails
- Using ; which ignores success/failure
Ready to prove it?
Pass the test and this pattern moves unknown
