TrumanWong

exit

Exit the current shell.

Summary

exit[n]

The main purpose

  • Executing exit causes the shell to exit with the specified status value. If no parameters are set, the return value of the last command will be used as the return value of exit.

Parameters

n (optional): Specified shell return value (integer).

return value

The return value is the value of the parameter n you specify. If the parameter you specify is greater than 255 or less than 0, then the return value will always be between 0 and 255 by adding or subtracting 256.

example

Exit the current shell:

[root@localhost ~]# exit
logout

You can also use ctrl+d to exit the current terminal. The following is a list of ways to turn this feature on or off:

#Open ctrl+d to exit the terminal
set -o ignoreeof
#Close ctrl+d to exit the terminal
set +o ignoreeof

In the script, enter the directory where the script is located, otherwise exit:

cd $(dirname $0) || exit 1

In the script, determine the number of parameters, print the usage method if they do not match, and exit:

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

In the script, delete the temporary files on exit:

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

Notice

  1. This command is a built-in bash command. For related help information, please see the help command.