Exit Status

Exit status is the numeric result a shell observes when a command finishes. Zero conventionally means success; other values have meanings defined by that command. A shell can also report failures to launch a program or termination by a signal, so the status is broader than a value deliberately returned by application code.

The text a program prints and the status it reports are separate outputs. Printing an error message and then exiting with zero reports success to a wrapper that checks only status. Conversely, a diagnostic on standard error can be informational. A scheduler cannot infer the intended outcome from the word ERROR alone.

In Bash, the special parameter $? holds the most recent command’s status. Save it immediately with status=$? if later commands need the value. The && operator runs the right-hand command only when the left-hand command succeeds. A nonzero result still requires interpretation: grep uses 1 for no match, which may be an acceptable empty result rather than a broken search.

For a foreground pipeline, Bash normally uses the final command’s status. Enabling pipefail makes it use the rightmost nonzero status, or zero when all commands succeed. It does not identify the cause or make partial writes disappear. Short readers such as head can also cause an upstream broken pipe. Define expected statuses and validate the resulting data before treating the job as complete.

Reference: GNU Bash: Exit Status and Pipelines. Practice these ideas in Linux, the Shell, and Git for Data Work.


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.