0
0
Linux CLIscripting~10 mins

Command chaining (&&, ||, ;) in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Command chaining (&&, ||, ;)
Start
Execute first command
Check exit status
If &&
Execute next command
If first failed?
Skip
End or next chain
This flow shows how the shell runs commands connected by &&, ||, or ; based on success or failure of previous commands.
Execution Sample
Linux CLI
echo Hello && echo World || echo Fail; echo Done
Runs 'echo Hello', then 'echo World' if first succeeds, else 'echo Fail', then always 'echo Done'.
Execution Table
StepCommandPrevious Exit StatusCondition CheckedAction TakenOutput
1echo HelloN/ARun first commandExecuteHello
2echo World0 (success)&&: previous success?ExecuteWorld
3echo Fail0 (success)||: previous failed?Skip
4echo DoneN/A;: always run nextExecuteDone
💡 All commands processed; chaining rules determined which commands ran.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Exit StatusN/A0000
Command ExecutedNoneecho Helloecho Worldecho World (skip echo Fail)echo Done
Key Moments - 3 Insights
Why does 'echo Fail' not run even though it is after 'echo World'?
Because 'echo World' succeeded (exit status 0), the '||' operator skips the next command if the previous succeeded, as shown in step 3 of the execution_table.
What is the difference between ';' and '&&' in chaining?
';' always runs the next command regardless of success or failure, while '&&' runs the next command only if the previous succeeded. See step 4 where 'echo Done' runs no matter what.
How does the shell decide to run the second command after '&&'?
It checks if the previous command's exit status is zero (success). If yes, it runs the next command. This is shown in step 2 where 'echo World' runs because 'echo Hello' succeeded.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the exit status after step 1?
A0 (success)
B1 (failure)
CN/A
DUndefined
💡 Hint
Check the 'Previous Exit Status' column for step 2 which shows the exit status after step 1.
At which step does the shell skip a command due to chaining rules?
AStep 2
BStep 4
CStep 3
DNo commands are skipped
💡 Hint
Look at the 'Action Taken' column in the execution_table to find where a command is skipped.
If the first command 'echo Hello' failed, which command would run next?
A'echo Fail' only
B'echo Fail' and 'echo Done'
C'echo World' and 'echo Fail'
D'echo World' only
💡 Hint
Consider how '&&' and '||' behave on failure, referencing the concept_flow and execution_table logic.
Concept Snapshot
Command chaining connects commands with operators:
- '&&' runs next only if previous succeeds
- '||' runs next only if previous fails
- ';' runs next always
Exit status (0=success) controls flow
Use chaining to combine commands smartly
Full Transcript
This lesson shows how Linux shell command chaining works using &&, ||, and ; operators. The shell runs the first command, then checks its exit status. If chained with &&, the next command runs only if the previous succeeded (exit status 0). If chained with ||, the next command runs only if the previous failed (non-zero exit status). The ; operator runs the next command always, regardless of previous success or failure. The example 'echo Hello && echo World || echo Fail; echo Done' runs 'echo Hello' first. Since it succeeds, 'echo World' runs next. Because 'echo World' succeeded, 'echo Fail' is skipped due to ||. Finally, 'echo Done' runs because ; always runs the next command. This chaining lets you control command flow based on success or failure, useful for scripting and command line tasks.