Q1When does cmd2 run in cmd1 && cmd2?
Chaining Commands — ; && || and Quotes
Learn sequential execution with ;, conditional execution with && (on success) and || (on failure), and how single vs double quotes differ in variable expansion — hands-on in a browser terminal.
Chaining Commands — ; && ||
You can chain several commands on one line.
; runs them in order regardless of the previous command's result.
&& runs the next command only when the previous one succeeded, and || runs the next only when the previous one failed.
| Symbol | Runs the next when | Example |
|---|---|---|
; | Always, regardless of the result | echo a ; echo b |
&& | Only if the previous succeeded (exit status 0) | mkdir d && cd d |
|| | Only if the previous failed (non-zero) | cat f || echo ng |
; always runs in sequence, && runs next on success, and || runs next on failure.echo step1 ; echo step2 # run both in order
mkdir build && echo created # created if it succeeds
ls missing.txt || echo not-found # not-found if it fails
Combining && and || — Branch Inline
You can write && and || one after another on a line.
cmd && echo ok || echo ng runs echo ok if cmd succeeds, and echo ng if it fails.
It's the go-to form when you want to write both the success and failure actions in one place.
cmd && echo ok || echo ng prints ok if cmd succeeds, or ng if it fails.mkdir out && echo created || echo failed # created on success
ls nofile.txt && echo found || echo none # none on failure
How Quotes Differ — ' and "
There are two kinds of quotes for wrapping a string.
Single quotes '...' treat the contents as literal characters and do not replace $VAR.
Double quotes "..." expand $VAR and $(...) inside to their values.
They're also used to pass a string containing spaces as a single unit.
| Quote | $VAR | $(...) | Example → output |
|---|---|---|---|
'...' (single) | Not expanded (literal) | Not expanded | echo '$name' → $name |
"..." (double) | Expanded to value | Expanded to command output | echo "$name" → Linux |
$variable to its value.place=server
echo 'path is $place' # literal: path is $place
echo "path is $place" # expanded: path is server
Embedding Variables and Command Output in Double Quotes
Inside double quotes you can build a single string from variables and command substitution together.
$(...) is replaced by the command's output.
For example, $(pwd) is replaced by the path of your current location before it's shown.
Wrapping in double quotes is also the basic way to pass a value containing spaces as a single argument.
What Success and Failure Really Are — Exit Status and echo $?
The 'success or failure' that && and || watch is determined by a number called the exit status that a command leaves when it finishes.
0 means success and anything other than 0 means failure.
The previous command's exit status goes into the special variable $?, which you can check with echo $?.
| Exit status | Meaning | Check |
|---|---|---|
0 | The previous command succeeded | echo $? shows 0 |
| non-zero | The previous command failed | echo $? shows 1, etc. |
echo $? is 0; right after one that fails, it's non-zero.ls . # a command that succeeds
echo $? # previous exit status -> 0
cat nofile.txt # a command that fails
echo $? # previous exit status -> non-zero (e.g. 1)
Knowledge Check
Answer each question one by one.
Q2What does echo '$name' print? (assume name=Linux is set)
Q3Which is correct about the role of ||?