LINUX / A CONCEPT NOTE

Exit Codes

how scripts know something failed

~60 sec read

Overview · mechanism
pitfall · examples

01 / THE SHORT VERSION

The idea in a few sentences.

Processes return an exit status code (0 to 255) to the parent shell. An exit code of 0 means success; anything else indicates an error.

02 / FOLLOW THE MECHANISM

How exit codes accumulate

  1. Process exit

    application exits calling return or exit(1).

  2. Parent trap

    kernel registers the exit code value in the process table.

  3. Query

    the shell captures the return value in the special variable $?.

  4. Chain check

    operators like && or || check this code to decide whether to trigger subsequent jobs.

04 / COMMAND NOTES

Read the command, then the result.

Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.

EXAMPLE 01 · REFERENCE

print the exit status code of the last executed command

echo $?

EXAMPLE 02 · REFERENCE

test exit code logic chaining

false || echo \"failed\"

Explore command anatomy in the CLI lab