An exit status (or return code) is an 8-bit unsigned integer value betweenDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
0 and 255 returned by an executed command, script, or shell function to indicate its termination state. External commands and scripts return this value to their parent process, whereas shell functions return it to the caller within the current shell execution environment. In Bash, an exit status of 0 denotes success, while any non-zero value (1 through 255) indicates an error or abnormal termination.
Accessing the Exit Status
Bash stores the exit status of the most recently executed foreground pipeline in the special parameter$?.
$? is overwritten after every command execution, it must be captured immediately into a variable if the value needs to be evaluated multiple times.
Setting the Exit Status
The exit status of a script or shell session is explicitly defined using theexit builtin. For shell functions and sourced scripts (e.g., source ./script.sh or . ./script.sh), the return builtin is used.
exit or return is invoked without an integer argument, or if a script/function terminates without an explicit exit/return statement, the exit status defaults to the exit status of the last executed command within that scope.
Because the exit status is an 8-bit integer, passing a numeric value outside the 0-255 range results in a modulo 256 operation (e.g., exit 256 evaluates to an exit status of 0, and exit -1 evaluates to 255).
Reserved Exit Codes
Bash and POSIX standards reserve specific non-zero exit codes to denote particular failure states.| Code | Meaning | Technical Trigger |
|---|---|---|
0 | Success | Command completed its intended operation. |
1 | General Error | Catchall for general, non-specific failures. |
2 | Misuse of shell builtins | Syntax error or missing keyword in a builtin command. |
126 | Cannot execute | Command is found, but lacks executable permissions. |
127 | Command not found | Command does not exist in $PATH or as a builtin/function. |
128 + N | Fatal error signal | Process was terminated by signal N (e.g., 130 for SIGINT/Ctrl+C, which is 128 + 2). |
255 | Non-numeric exit argument | exit was passed a non-integer argument (e.g., exit foo). |
Exit Status in Pipelines
By default, the exit status of a pipeline is the exit status of the last (rightmost) command in the chain.PIPESTATUS array variable. It contains a list of exit status values corresponding to the processes in the most recently executed foreground pipeline.
Like $?, the PIPESTATUS array is highly volatile and is overwritten by the execution of the very next command. If a developer attempts to evaluate its elements sequentially (e.g., across multiple if statements or echo commands), the array will be overwritten after the first check. To evaluate multiple values, the entire array must be captured immediately.
pipefail option is enabled via set -o pipefail, the pipeline’s exit status becomes the value of the last (rightmost) command to exit with a non-zero status. If all commands exit successfully, the pipeline returns 0.
Logical Evaluation and Negation
Bash control structures (if, while, until) and logical operators (&&, ||, !) evaluate the exit status of commands directly. They do not evaluate boolean strings; they evaluate the integer returned to the shell.
&&(AND): Executes the right-hand command only if the left-hand command returns0.||(OR): Executes the right-hand command only if the left-hand command returns a non-zero value.!(NOT): Inverts the exit status of a pipeline. If the command returns0,!changes the exit status to1. If the command returns a non-zero value,!changes it to0.
true and false builtins exist solely to return specific exit statuses for these evaluations. true always returns 0, and false always returns 1.
Automatic Termination on Error
To enforce strict error handling based on exit statuses, Bash provides theerrexit option. Enabled via set -e or set -o errexit, this setting instructs the shell to terminate immediately if any pipeline, list, or compound command yields a non-zero exit status.
errexit option does not trigger termination if the failing command is part of a condition in an if, while, or until statement, part of a && or || list (except the final command), or if its exit status is explicitly inverted using the ! operator.
Master Bash with Deep Grasping Methodology!Learn More





