Learn by reading through in order

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.

SymbolRuns the next whenExample
;Always, regardless of the resultecho 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
How ; && || differ
echo A ; echo Bruns both A and B regardlesscmd && echo okok only if cmd succeedscmd || echo ngng only if cmd fails
; 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

① Run echo first; echo second and confirm ; prints the two outputs in order.

② Run echo ok && echo done and confirm && continues to the next when the previous succeeds.

③ Run cat nofile.txt || echo missing and confirm || runs the right side only when the previous fails. (Run it correctly and an explanation will appear.)

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

Branching by combining && and ||
cmdbranch on exit status&& echo okok if cmd succeeds|| echo ngng if the previous failed
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

① Run ls . && echo found || echo none and confirm found appears because ls . succeeds.

② Run cat nofile.txt && echo found || echo none and confirm none appears because cat fails.

③ Compare the two results and see how the part after && and the part after || switch based on success or failure.

Linux console
0 / 2 completed
Loading Linux Terminal...

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 expandedecho '$name'$name
"..." (double)Expanded to valueExpanded to command outputecho "$name"Linux
Single vs double expansion
name=Linuxset name to Linuxecho '$name'prints $name literallyecho "$name"expands to Linux
Single quotes stay literal; double quotes expand $variable to its value.
place=server
echo 'path is $place'   # literal: path is $place
echo "path is $place"   # expanded: path is server

① Set a variable with name=Linux.

② Run echo '$name' and confirm single quotes print $name literally.

③ Run echo "$name" and confirm double quotes expand it to the value Linux.

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

① Set a variable with user=guest.

② Run echo "hello $user" && echo done and confirm the variable expands inside double quotes and done follows on success.

③ Run echo "now: $(pwd)" and confirm the $(...) inside double quotes is replaced by the current path.

Linux console
0 / 3 completed
Loading Linux Terminal...

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 statusMeaningCheck
0The previous command succeededecho $? shows 0
non-zeroThe previous command failedecho $? shows 1, etc.
Reading exit status with echo $?
ls . then echo $?0 (success)cat nofile then echo $?non-zero (failure)
Right after a command that succeeds, 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)

① Run ls . and then echo $?, and confirm it shows 0 because it succeeded.

② Run cat nofile.txt and then echo $?, and confirm it shows non-zero (like 1) because it failed.

③ Notice that the number from echo $? is what && and || base their decision on.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1When does cmd2 run in cmd1 && cmd2?

Q2What does echo '$name' print? (assume name=Linux is set)

Q3Which is correct about the role of ||?