exit - Unix, Linux Command



exit-The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent process.




exit-Issuing the exit command at the shell prompt will cause the shell to exit.

In some cases, if you have jobs running in the background, the shell will remind you that they are running and simply return you to the command prompt. In this case, issuing exit again will terminate those jobs and exit the shell. Common aliases for exit include "bye", "logout", and "lo".Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 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 exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).



To exit from shell:

$ exit

# su ubuntu

ubuntu@ubuntu:~$ exit


exit command is used to return the succsess/failure of functionality in script.

# cat test.sh

echo "This is a test."
# Terminate our shell script with success message
exit 0

root@ubuntu:/home/ubuntu# ./test.sh
This is a test.root@ubuntu:/home/ubuntu# echo $?

root@ubuntu:/home/ubuntu# cat test.sh
echo "This is a test."
# Terminate our shell script with failure message
exit 1

root@ubuntu:/home/ubuntu# ./test.sh
This is a test.
root@ubuntu:/home/ubuntu# echo $?