Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

exit-codes-bash

Exit codes in Bash

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Exit codes in Bash: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Exit codes in Bash in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Exit codes in Bash in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons. Also check $? after commands; scripts should exit non-zero on failure.

When you can explain the previous lesson's ideas without copying starter code.

Every command returns an exit status (0 = success, non-zero = failure). Scripts and CI pipelines use exit codes to decide whether a deploy should continue.

Checking status

ls /tmp
echo "status: $?"
ls /nonexistent 2>/dev/null
echo "status: $?"

$? holds the last command's exit code—read it immediately before another command runs.

Explicit return

#!/usr/bin/env bash
if [[ $# -lt 1 ]]; then
  echo "usage: $0 name" >&2
  exit 1
fi
echo "Hello, $1"
exit 0

Send errors to stderr with >&2 so stdout stays machine-readable.

Logical operators

false && echo "won't run"
true || echo "won't run"
false || echo "runs because previous failed"

&& and || short-circuit like many languages—common in CI one-liners.

Important interview questions and answers

  1. Q: What exit code means success?
    A: 0—by Unix convention.
  2. Q: Why check $? quickly?
    A: Another command overwrites $? immediately.

Self-check

  1. What does $? contain?
  2. What exit code does false typically return?

Tip: In CI, non-zero exits fail the job—always check $? when not using set -e.

Interview prep

Success code?

0 by convention.

$? meaning?

Exit status of the last foreground command.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • $? timing?
  • set -e tradeoff?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump