> > >Exit and Exit Status > NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ "> REL="HOME" TITLE="Advanced Bash-Scripting Guide" HREF="index.html"> REL="UP" TITLE="Basics" HREF="part2.html"> REL="PREVIOUS" TITLE="Escaping" HREF="escapingsection.html"> REL="NEXT" TITLE="Tests" HREF="tests.html"> HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> REL="stylesheet" HREF="common/kde-common.css" TYPE="text/css"> HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> HTTP-EQUIV="Content-Language" CONTENT="en"> REL="stylesheet" HREF="common/kde-localised.css" TYPE="text/css" TITLE="KDE-English"> REL="stylesheet" HREF="common/kde-default.css" TYPE="text/css" TITLE="KDE-Default"> > CLASS="CHAPTER" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#AA0000" VLINK="#AA0055" ALINK="#AA0000" STYLE="font-family: sans-serif;" > CLASS="NAVHEADER" > SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" > > COLSPAN="3" ALIGN="center" >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting > > > WIDTH="10%" ALIGN="left" VALIGN="bottom" > HREF="escapingsection.html" ACCESSKEY="P" >Prev > > WIDTH="80%" ALIGN="center" VALIGN="bottom" > > WIDTH="10%" ALIGN="right" VALIGN="bottom" > HREF="tests.html" ACCESSKEY="N" >Next > > > > ALIGN="LEFT" WIDTH="100%"> > CLASS="CHAPTER" > > NAME="EXIT-STATUS" > >Chapter 6. Exit and Exit Status > BORDER="0" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" CLASS="EPIGRAPH" > > WIDTH="45%" > > WIDTH="45%" ALIGN="LEFT" VALIGN="TOP" > > > >...there are dark corners in the Bourne shell, and people use all of them. > > > >--Chet Ramey > > > > > > > NAME="EXITCOMMANDREF" > >The CLASS="COMMAND" > exit > command may be used to terminate a script, just as in a CLASS="FIRSTTERM" >C > program. It can also return a value, which is available to the script's parent process. > > NAME="EXITSTATUSREF" > >Every command returns an CLASS="FIRSTTERM" > exit status > (sometimes referred to as a CLASS="FIRSTTERM" > return status > or CLASS="FIRSTTERM" >exit code >). NAME="EXITSUCCESS" > > A successful command returns a CLASS="RETURNVALUE" >0 >, while an unsuccessful one returns a CLASS="RETURNVALUE" >non-zero > value that usually may be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a CLASS="RETURNVALUE" >0 > exit code upon successful completion, though there are some exceptions. > >Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an CLASS="USERINPUT" > >exit CLASS="REPLACEABLE" > >nnn > > > > command may be used to deliver an CLASS="RETURNVALUE" > CLASS="REPLACEABLE" > >nnn > > > exit status to the shell ( CLASS="RETURNVALUE" > CLASS="REPLACEABLE" > >nnn > > > must be a decimal number in the CLASS="RETURNVALUE" >0 > - CLASS="RETURNVALUE" >255 > range). > CLASS="NOTE" > CLASS="NOTE" WIDTH="100%" BORDER="0" > > WIDTH="25" ALIGN="CENTER" VALIGN="TOP" > SRC="common/note.png" HSPACE="5" ALT="Note"> > ALIGN="LEFT" VALIGN="TOP" > >When a script ends with an CLASS="COMMAND" >exit > that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the CLASS="COMMAND" >exit >). > > BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" > > > CLASS="PROGRAMLISTING" > 1 #!/bin/bash 2 3 COMMAND_1 4 5 . . . 6 7 # Will exit with status of last command. 8 COMMAND_LAST 9 10 exit > > > > > >The equivalent of a bare CLASS="COMMAND" >exit > is CLASS="COMMAND" >exit $? > or even just omitting the CLASS="COMMAND" >exit >. > > BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" > > > CLASS="PROGRAMLISTING" > 1 #!/bin/bash 2 3 COMMAND_1 4 5 . . . 6 7 # Will exit with status of last command. 8 COMMAND_LAST 9 10 exit $? > > > > > > BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" > > > CLASS="PROGRAMLISTING" > 1 #!/bin/bash 2 3 COMMAND1 4 5 . . . 6 7 # Will exit with status of last command. 8 COMMAND_LAST > > > > > > > > > > NAME="EXSREF" > > > > CLASS="VARNAME" > $? > reads the exit status of the last command executed. After a function returns, CLASS="VARNAME" >$? > gives the exit status of the last command executed in the function. This is Bash's way of giving functions a CLASS="QUOTE" >"return value." > After a script terminates, a CLASS="VARNAME" >$? > from the command line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, CLASS="USERINPUT" > >0 > > on success or an integer in the range CLASS="RETURNVALUE" >1 - 255 > on error. > CLASS="EXAMPLE" > NAME="EX5" > > > >Example 6-1. exit / exit status > > BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" > > > CLASS="PROGRAMLISTING" > 1 #!/bin/bash 2 3 echo hello 4 echo $? # Exit status 0 returned because command executed successfully. 5 6 lskdf # Unrecognized command. 7 echo $? # Non-zero exit status returned because command failed to execute. 8 9 echo 10 11 exit 113 # Will return 113 to shell. 12 # To verify this, type "echo $?" after script terminates. 13 14 # By convention, an 'exit 0' indicates success, 15 #+ while a non-zero exit value means an error or anomalous condition. > > > > > > HREF="variables2.html#XSTATVARREF" >$? > is especially useful for testing the result of a command in a script (see HREF="filearchiv.html#FILECOMP" >Example 15-34 > and HREF="textproc.html#LOOKUP" >Example 15-19 >). > CLASS="NOTE" > CLASS="NOTE" WIDTH="100%" BORDER="0" > > WIDTH="25" ALIGN="CENTER" VALIGN="TOP" > SRC="common/note.png" HSPACE="5" ALT="Note"> > ALIGN="LEFT" VALIGN="TOP" > >The HREF="special-chars.html#NOTREF" >! >, the CLASS="FIRSTTERM" >logical not > qualifier, reverses the outcome of a test or command, and this affects its HREF="exit-status.html#EXITSTATUSREF" >exit status >. CLASS="EXAMPLE" > NAME="NEGCOND" > > > >Example 6-2. Negating a condition using CLASS="TOKEN" >! > > > BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" > > > CLASS="PROGRAMLISTING" > 1 true # The "true" builtin. 2 echo "exit status of \"true\" = $?" # 0 3 4 ! true 5 echo "exit status of \"! true\" = $?" # 1 6 # Note that the "!" needs a space between it and the command. 7 # !true leads to a "command not found" error 8 # 9 # The '!' operator prefixing a command invokes the Bash history mechanism. 10 11 true 12 !true 13 # No error this time, but no negation either. 14 # It just repeats the previous command (true). 15 16 # Thanks, St閜hane Chazelas and Kristopher Newsome. > > > > > > > > > > CLASS="CAUTION" > CLASS="CAUTION" WIDTH="100%" BORDER="0" > > WIDTH="25" ALIGN="CENTER" VALIGN="TOP" > SRC="common/caution.png" HSPACE="5" ALT="Caution"> > ALIGN="LEFT" VALIGN="TOP" > >Certain exit status codes have HREF="exitcodes.html#EXITCODESREF" >reserved meanings > and should not be user-specified in a script. > > > > > > CLASS="NAVFOOTER" > ALIGN="LEFT" WIDTH="100%"> SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" > > WIDTH="33%" ALIGN="left" VALIGN="top" > HREF="escapingsection.html" ACCESSKEY="P" >Prev > > WIDTH="34%" ALIGN="center" VALIGN="top" > HREF="index.html" ACCESSKEY="H" >Home > > WIDTH="33%" ALIGN="right" VALIGN="top" > HREF="tests.html" ACCESSKEY="N" >Next > > > > WIDTH="33%" ALIGN="left" VALIGN="top" >Escaping > WIDTH="34%" ALIGN="center" VALIGN="top" > HREF="part2.html" ACCESSKEY="U" >Up > > WIDTH="33%" ALIGN="right" VALIGN="top" >Tests > > > > > >