Search code examples
linuxbashinit.dcentos6.5

Why do many init.d scripts end in "exit $?"?


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?


Solution

  • 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.