In shell-script, what is this command? exit $?
I didn't find any resource to explain it.
In shell-script, what is this command? exit $?
I didn't find any resource to explain it.
The intention with exit $?
is generally to exit the shell with the same exit status as the previous command (whose $?
is the shell's representation). However it has a few issues:
$?
, which means it will be subject to split+glob so would not work properly if $?
contained characters of $IFS
.$?
was negative, like -12
, as possible if the previous command was a function or builtin in some shells, that could be interpreted by exit
as an unrecognised option and give an error. exit -- "$?"
could avoid it but --
is not supported by all shells.Those two problems above can be avoided if we just use exit
alone as it just happens that the default behaviour of exit
when not given any argument is to exit with the last command's exit status.
You'll find that a common idiom to exit a script when the previous command failed (and exit with the same failure exit status of that command) is to do:
that-command || exit
There are further considerations when the previous command died of a signal or when the return code from the previous command was outside the range 0 to 127, most of which are described at: Default exit code when process is terminated?
exit
portion of this question. @shell-script, please let me know if the linked question does not answer yours. – Jeff Schaller May 17 '20 at 16:37