From bash manual
The exit status of an executed command is the value returned by the
waitpidsystem call or equivalent function. Exit statuses fall between 0 and 255, though, as explained below, the shell may use values above 125 specially. Exit statuses from shell builtins and compound commands are also limited to this range. Under certain circumstances, the shell will use special values to indicate specific failure modes.
What determines the exit status of an executed command,
- the command itself, i.e. does the implementation of the command determines the exit status, i.e. is determination of the exit status of an executed command an inherent property of the command, just like parsing the command line arguments to an execution of a command is inherent to the command, or
- the shell process which executed the command?
waitpidsystem call mentioned above seems to me that the exit status of an executed command is implemented by the shell process, and the command itself has nothing to do with the exit status of an execution of itself.
In a bash process, when can we get the exit status of an executed command,
- only when the bash process is interactive, or
- regardless of whether the bash process is interactive or non-interactive? When a bash process is non-interactive, how can we retrieve the exit status of a command executed in the bash process?