Ending the execution
How to exit: exit()
If you are running a script and want to stop execution and return to the shell the right way, you should use the exit()
function.
Why?
- with the
exit()
function you can specify the exit status code. The status0
means βeverything is fineβ, the status value greater than 0 means something went wrong with the execution; - with
exit()
function it is possible to specify a string as parameter, printed at the end of the execution; - when the
exit()
function is called, a callback function is automatically executed. The function executed is defined withregister_shutdown_function()
.
Exit parameter
The exit code is a convention to communicate the exit status of your execution to the shell that started executing the script.
For example, in your shell environment you can detect the exit code via $?
shell variable.