Exit status, true, false and exit
An exit status is a number reporting how a command completed. Bash treats zero as success and nonzero as failure or another command-defined outcome. true returns 0; false returns 1; neither normally prints text. $? expands to the most recent command status; assign it immediately to preserve it. exit n ends the current shell with status n (use integers 0–255); without n it uses the previous command status. Use a child shell bash -c '...' to practice exit without closing your terminal. Bash conventionally reports command-not-found as 127 and found-but-not-executable as 126; termination by fatal signal N is reported as 128+N. These codes do not uniquely identify every cause because programs can choose overlapping values. Conditional handling comes next.
Practical examples
Run each example separately in Bash 5.2 in a fresh empty practice directory, using default shell options and C locale. Output is shown separately; do not enter it as code.
Silent statuses
Expected stdout:
true=0 false=1Expected stderr:
No output
Overall example exit status: 0
The builtins themselves are silent. The final printf displays the saved statuses.
Child exit
Expected stdout:
child=7Expected stderr:
No output
Overall example exit status: 0
Only the child exits; the parent can report its status.
Warning does not mean failure
bash -c 'printf "%s\n" warning >&2; exit 0'a=$?bash -c 'exit 5'b=$?printf 'first=%s second=%s\n' "$a" "$b"Expected stdout:
first=0 second=5Expected stderr:
warningOverall example exit status: 0
The first child emits a diagnostic but succeeds; the second fails silently.
Common mistakes
Do not infer success from stdout or failure from stderr. Do not inspect $? after another command has replaced it. Do not run exit directly in your interactive shell unless you intend to end it.
Practice
Run two child shells that return 0 and 5. Make the successful child print a warning to stderr, and the unsuccessful child remain silent. Capture and print both statuses.
Next: Conditions and comparisons