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?

Score_Under
  • 1,189
  • 10
  • 20

1 Answers1

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.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • So it is for code clarity? (A snippet saying "Don't overwrite the exit code because I am relying on it"?) – Score_Under Aug 18 '14 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 '14 at 11:03