Ad space (banner)
Lesson 13 / 20

Exit status and && / ||

This lesson covers the exit status that reports whether a command succeeded, and the && and || operators built on it.

Every command returns a number — its exit status — when it finishes. Zero means success and anything else means failure. && runs the next command only if the previous one succeeded; || runs it only if the previous one failed.

The sample code writes mkdir backup && echo "Created successfully", displaying the message only when the folder was created.

A common early stumble is confusing && and || with the logical operators of other languages. What is distinctive here is that the branch is on whether a command succeeded, not on whether a value is true.

In professional work, they are used to build safe sequences: continue only if the backup succeeded, print an error and stop if a command failed.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Shell Script directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)