exit(1) exit(1)
NAME
exit - terminate a shell script
SYNOPSIS
exit [exitvalue]
DESCRIPTION
The shell built-in exit is used to terminate shell scripts. If you
wish, you can define an exit status for the terminated script by fol-
lowing 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, the shell in which you are currently
working terminates.
OPERANDS
exitvalue
Any number between 0 and 127. This number defines the exit status
with which the shell script terminates.
The exit value can be checked with echo $?.
exitvalue not specified:
The exit value of the command executed before the exit call is
used.
EXAMPLES
Example 1
The false command can be implemented by means of the following shell
script:
: Shell version of the false command
: Exit status always 1
exit 1
Example 2
The two shell scripts named end and check will be used to demonstrate
how the exit status can be evaluated.
Page 1 Reliant UNIX 5.44 Printed 11/98
exit(1) exit(1)
- 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|pg;;
4) echo Entering $1 is meaningless for this script!
esac
The shell script check is now initiated:
$ sh check .profile
Contents of file .profile:
HOME=/usr1/rose/src
export HOME
.
.
.
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.
Page 2 Reliant UNIX 5.44 Printed 11/98
exit(1) exit(1)
This shell script endaction shows how the exit status can be inter-
preted 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 of file $1:; cat $1;;
3) echo Contents of $1:; ls -l $1|pg;;
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 speci-
fied 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.
NOTES
Some differences in behavior may occur when using exit, depending on
which shell is being used. The possible differences are not described
specifically.
SEE ALSO
false(1), ksh(1), sh(1), exit(2).
Page 3 Reliant UNIX 5.44 Printed 11/98