Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An exit status (or return code) is an 8-bit unsigned integer value between 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 $?.
command
echo $?
Because $? is overwritten after every command execution, it must be captured immediately into a variable if the value needs to be evaluated multiple times.
command
STATUS=$?

Setting the Exit Status

The exit status of a script or shell session is explicitly defined using the exit builtin. For shell functions and sourced scripts (e.g., source ./script.sh or . ./script.sh), the return builtin is used.

# Terminate script with a specific status
exit 1


# Terminate function or sourced script with a specific status
return 0
If 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.
CodeMeaningTechnical Trigger
0SuccessCommand completed its intended operation.
1General ErrorCatchall for general, non-specific failures.
2Misuse of shell builtinsSyntax error or missing keyword in a builtin command.
126Cannot executeCommand is found, but lacks executable permissions.
127Command not foundCommand does not exist in $PATH or as a builtin/function.
128 + NFatal error signalProcess was terminated by signal N (e.g., 130 for SIGINT/Ctrl+C, which is 128 + 2).
255Non-numeric exit argumentexit 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.
command1 | command2 | command3

# $? contains the exit status of command3
To access the exit statuses of all commands within a pipeline, Bash provides the 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.
command1 | command2 | command3


# Capture the entire array immediately
statuses=("${PIPESTATUS[@]}")


# Elements can now be evaluated safely
echo "Status of command1: ${statuses[0]}"
echo "Status of command2: ${statuses[1]}"
echo "Status of command3: ${statuses[2]}"
If the 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 returns 0.
  • || (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 returns 0, ! changes the exit status to 1. If the command returns a non-zero value, ! changes it to 0.

# Logical AND / OR
command1 && command2
command1 || command2


# Logical NOT
! command

# $? is 1 if command succeeded, 0 if command failed


# Control structure evaluation
if ! command; then
    # Executes if command returns a non-zero status
fi
The 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 the errexit 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.
set -e
command_that_fails  # Script terminates immediately here
command_skipped     # This will not execute
The 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