Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Exit Codes · Bash Automation

Direct answer

Exit codes are the number a command returns: 0 for success and 1–255 for failure, available in $?. Scripts should exit non-zero on errors so callers and schedulers detect failures.

When to use this

Use this when scripts must report success or failure correctly.

First 30 seconds

1. Exit 0 = success; non-zero = failure.

2. Check codes explicitly for important commands.

3. set -e has exceptions (conditions, pipelines without pipefail).

4. Document meaningful codes.

5. Callers (cron, systemd, CI) rely on them.

Operational verification

command → exit code → decision

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
GNU Bash shell automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformGNU Bash shell automation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when scripts must report success or failure correctly.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Verification path Verify boundary

The negative fixture should deliberately exercise risks such as unquoted expansion, globbing, pipeline status loss, subshell scope, set -e misconceptions, signal races, temporary-file leakage and destructive path ambiguity, choosing the cases most relevant to process exit status as a contract, reserved/application-specific codes, pipeline/wrapper propagation and keeping stderr/output interpretation separate.

Step 2 Common mistakes Narrow the fault
  • Scripts that always exit 0.
  • set -e surprises inside if and pipelines — test them.
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • 0 is success; non-zero is failure.
  • $? holds the last exit code.
  • Exit non-zero on errors.
Key checks
  • echo $? — last exit code
  • set -e — exit on error
  • exit 3 — explicit code
ReferenceWhy it matters1 section · depth on demand

What it is

Every command returns an exit code: 0 for success, non-zero for failure. Scripts should check them and exit non-zero on failure so schedulers and CI notice.

ReferenceImportant terms & boundaries2 sections · depth on demand

In depth

Exit codes are how Bash scripts report success (0) or failure (non-zero) to callers, schedulers and CI. A script's exit code is that of the last command run unless it exits explicitly, which can hide failures.

#!/usr/bin/env bash
set -euo pipefail

readonly E_USAGE=2 E_UNREACHABLE=3
[[ $# -eq 1 ]] || { echo "usage: $0 host" >&2; exit $E_USAGE; }
ping -c1 -W2 "$1" >/dev/null || exit $E_UNREACHABLE

if ! out=$(ssh "$1" 'show version' 2>&1); then
  echo "ssh failed: $out" >&2; exit 1
fi

With set -e, commands in if conditions, || and && lists do not stop the script, which is how expected failures are handled explicitly. Document custom exit codes so callers can react to specific failures.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docGNU Bash shell automation current

Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Exit Codes. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%
Depth when you need it

Deep reference

Healthy vs suspicious evidence

Output interpretation

For process exit status as a contract, reserved/application-specific codes, pipeline/wrapper propagation and keeping stderr/output interpretation separate, prefer typed fields and stable IDs over human presentation text.

Confirmation

Confirm action

Continue?