01 / GETTING STARTED · LESSON 01

Commands, help and exit status

Run commands and capture an exit status before another command replaces it.

13 min · learn at your pace

A shell starts work

Bash reads a command name followed by arguments. In printf '%s\n' 'Hello, operator', printf is the command; the format and message are two separate arguments. Quotes keep the message together. The quotes are Bash syntax, not characters passed to the command.

Some commands are built into Bash. help printf explains the builtin; type printf tells you how Bash resolves the name. External programs such as cat run as separate processes. This course uses real Bash, not a POSIX sh substitute.

The other result

A command produces an exit status as well as any text: zero means success; nonzero means a failure or another documented condition. $? contains the most recent status. Even a successful printf replaces it, so save the status immediately when you need it later. false deliberately returns status 1 without printing an error.

The browser runs Bash 5.2.37, but required scripts use syntax available in macOS Bash 3.2. Try examples freely; Check supplies a clean filesystem and grades behavior, not the exact spelling of your answer.

Try an example.sh
help printf
false
status=$?
printf 'status=%s\n' "$status"
YOUR TURN

Put it into practice.

Print exactly `Hello, operator` and then `status=1`, each on its own line. Run `false` and report its exit status. The script itself must finish successfully.

See an explained solution
One way to solve it.sh
printf '%s\n' 'Hello, operator'
false
status=$?
printf 'status=%s\n' "$status"

The assignment saves the failing command’s status. The final printf succeeds, so the overall script exits zero while still reporting that earlier status.