Exit Status
man bash
$ man bash
可以找到
EXIT STATUS
The exit status of an executed command is the value returned by the waitpid system 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 circum‐
stances, the shell will use special values to indicate specific failure modes.
For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of zero
indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal signal N,
bash uses the value of 128+N as the exit status.
If a command is not found, the child process created to execute it returns a status of 127. If a command is
found but is not executable, the return status is 126.
If a command fails because of an error during expansion or redirection, the exit status is greater than zero.
Shell builtin commands return a status of 0 (true) if successful, and non-zero (false) if an error occurs while
they execute. All builtins return an exit status of 2 to indicate incorrect usage.
Bash itself returns the exit status of the last command executed, unless a syntax error occurs, in which case
it exits with a non-zero value. See also the exit builtin command below.
可以找到
ARGUMENTS
If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first
argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0
is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads
and executes commands from this file, then exits. Bash's exit status is the exit status of the last command
executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open
the file in the current directory, and, if no file is found, then the shell searches the directories in PATH
for the script.
Bash Reference Manual
2 Definitions / exit status
exit status
The value returned by a command to its caller. The value is restricted to eight bits, so the maximum value is 255.
3 Basic Shell Features / 3.7 Executing Commands / 3.7.5 Exit Status
3.7.5 Exit Status
The exit status of an executed command is the value returned by the waitpid system 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.
For the shell’s purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status.
If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.
If a command fails because of an error during expansion or redirection, the exit status is greater than zero.
The exit status is used by the Bash conditional commands (see Conditional Constructs) and some of the list constructs (see Lists).
All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. All builtins return an exit status of 2 to indicate incorrect usage.