The POSIX shell built-in exit is used to terminate shell scripts. If you wish, you can define an exit status for the terminated script by following exit with a number.
Without exit, shell scripts terminate:
when the last command has been executed, i.e. when the executing shell detects an end-of-file (EOF). The exit status is then the status of the last command executed.
when the executing shell cannot find a command or detects a syntax error; the exit status is then non-zero.
If you call exit interactively, either the shell (or subshell) in which you are currently working or the POSIX shell terminates.
Syntax
exit[ n] |
Any number between 0 and 256. This number defines the Exit status with which the shell script terminates. As described in other sections, certain exit status values are reserved for specific purposes. Applications may only use these values for these purposes:
trap is executed for EXIT before the shell terminates. This fails to occur only if exit itself is called in this trap. In this latter case, the shell terminates immediately. You can use the command echo $? to query the exit status. n not specified: |
Exit status
The exit status is n if it is set. Otherwise the exit status is the exit status of the last command executed or zero if no command has been executed. If exit is executed as part of a trap then the last command executed is taken to be the command which was executed immediately before the trap. |
Example 1
The false command can be implemented by means of the following shell script: : Shell version of the false command : Exit status allways 1 exit 1 |
Example 2
The two shell scripts named end and check will be used to demonstrate how the exit statuscan be evaluated: The end script contains the following: : Invocation with sh end file if [ -f "$1" ] then exit 2 elif [ -d "$1" ] then exit 3 else exit 4 fi The check file is scripted as follows: : Invocation with sh check file sh end "$1" case $? in 2) echo Contents of file $1:; cat $1;; 3) echo Contents of $1:; ls -l $1 4) echo Entering $1 is meaningless for this script! esac The shell script check is now initiated:
The shell script check calls the script named end. This script returns an exit status of 2, since .profile is a normal file. The commands specified in the case statement under the matching pattern 2 are then executed. The contents of the above two shell scripts cannot be simply combined into a single file, since the exit command terminates the script. This shell script endaction shows how the exit status can be interpreted within a script: : Invocation with sh endaction file (if [ -f "$1" ] then exit 2 elif [ -d "$1" ] then exit 3 else exit 4 fi) case $? in 2) echo Contents the file $1:; cat $1;; 3) echo Content of $1:; ls -l $1;; 4) echo Entering $1 is meaningless for this script! esac The parentheses around the if statement cause a subshell to be invoked. This subshell terminates with the exit status that is specified in the relevant exit command. Control is subsequently returned to the parent shell, i.e. the shell that processes the endaction script. This shell executes the remaining commands. |
See also
false |