Chapter 6. Exit and Exit Status
| ... there are dark corners in the Bourne shell, and people use all of them. --Chet Ramey |
The exit command terminates a script, just as in a Cprogram. It can also return a value, which is available to thescript's parent process.
Every command returns an exit status (sometimes referred to as a return status or exit code).A successful command returns a 0, whilean unsuccessful one returns a non-zerovalue that usually can be interpreted as an errorcode. Well-behaved UNIX commands, programs, andutilities return a 0 exit code uponsuccessful completion, though there are some exceptions.
Likewise, functionswithin a script and the script itself return an exitstatus. The last command executed in the function orscript determines the exit status. Within a script, anexit nnncommand may be used to deliver annnnexit status to the shell(nnnmust be an integer in the 0 -255 range).
| When a script ends with an exit that hasno parameter, the exit status of the script is the exit statusof the last command executed in the script (previous to theexit). #!/bin/bashCOMMAND_1. . .COMMAND_LAST# Will exit with status of last command.exit | The equivalent of a bare exit is exit $? or even just omitting theexit. #!/bin/bashCOMMAND_1. . .COMMAND_LAST# Will exit with status of last command.exit $? | #!/bin/bashCOMMAND1. . . COMMAND_LAST# Will exit with status of last command. | |
$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value."
Following the execution of a pipe, a $? gives the exit status of the last command executed.
After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.
Example 6-1. exit / exit status
#!/bin/bashecho helloecho $? # Exit status 0 returned because command executed successfully.lskdf # Unrecognized command.echo $? # Non-zero exit status returned -- command failed to execute.echoexit 113 # Will return 113 to shell. # To verify this, type "echo $?" after script terminates.# By convention, an 'exit 0' indicates success,#+ while a non-zero exit value means an error or anomalous condition.# See the "Exit Codes With Special Meanings" appendix. |
$? is especially useful for testing the result of a command in a script (see Example 16-35 and Example 16-20).
| The !, the logicalnot qualifier, reverses the outcome of a test orcommand, and this affects its exitstatus. Example 6-2. Negating a condition using ! true # The "true" builtin.echo "exit status of "true" = $?" # 0! trueecho "exit status of "! true" = $?" # 1# Note that the "!" needs a space between it and the command.# !true leads to a "command not found" error## The '!' operator prefixing a command invokes the Bash history mechanism.true!true# No error this time, but no negation either.# It just repeats the previous command (true).# =========================================================== ## Preceding a _pipe_ with ! inverts the exit status returned.ls | bogus_command # bash: bogus_command: command not foundecho $? # 127! ls | bogus_command # bash: bogus_command: command not foundecho $? # 0# Note that the ! does not change the execution of the pipe.# Only the exit status changes.# =========================================================== ## Thanks, St�phane Chazelas and Kristopher Newsome. | |
| Certain exit status codes have reserved meanings and should notbe user-specified in a script. |