Report issue Add example

exit

Exit the current shell.

Synopsis

exit [n]

Description

Parameters

n (optional): The specified shell return value (an integer).

Return Value

Returns the value specified by n. If n is greater than 255 or less than 0, the value is normalized to the range 0 to 255 (e.g., by adding or subtracting 256).

Examples

Exit the current shell:

[root@localhost ~]# exit
logout

You can also use Ctrl+D to exit the terminal. Here is how to enable or disable this feature:

# Enable Ctrl+D to exit terminal
set +o ignoreeof
# Disable Ctrl+D to exit terminal
set -o ignoreeof

In a script, enter the script's directory or exit:

cd $(dirname $0) || exit 1

In a script, check the number of arguments; exit if it doesn't match:

if [ "$#" -ne "2" ]; then
    echo "usage: $0 <area> <hours>"
    exit 2
fi

In a script, delete temporary files upon exiting:

trap "rm -f tmpfile; echo Bye." EXIT

Check the exit code of the previous command:

./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
    echo "O.K"
fi

Note

  1. This is a bash built-in command. For more help, use the help command.