5

I've seen a lot of strange quirks in CentOS 6.5's init.d scripts, but one pattern I've seen at the end of most of these scripts is

case "$1" in
    # ... commands here
esac
exit $?

What is the purpose of "exit $?" here?

1 Answer 1

5

It makes the script return the return code of the last significant command to the calling init system. Whenever a command exits, its return code is stored on $? by the shell.

It's actually not really necessary to explicitly specify $? but scripters probably just include it to be clear about what it intends to do.

exit: exit [n]

Exit the shell.

Exits the shell with a status of N. If N is omitted, the exit status is that of the last command executed.

I also hope you don't actually mean eend $? of OpenRC:

eend retval [string ]

If retval does not equal 0 then output the string using eerror and !! in square > brackets at the end of the line. Otherwise output ok in square brackets at the end of the line. The value of retval is returned.

See source.

2
  • So it is for code clarity? (A snippet saying "Don't overwrite the exit code because I am relying on it"?) Aug 18, 2014 at 10:58
  • That could be one thing. And if it's in the end of the script, exit can also be just omitted as the shell would return by default whatever value $? has.
    – konsolebox
    Aug 18, 2014 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.