Process Termination: Exit Status#

The Shell: A Program Like Any Other Program#

  • The shell is a program just like any other program (/bin/bash)

  • Main purpose: start other programs, and report on their exit status

$ grep jfasch /etc/passwd
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
$ echo $?            # <-- $? ... exit status of last foreground process (huh?)
0

The Shell: A Programming Language#

$ if grep jfasch /etc/passwd; then echo YAY; else echo NOPE; fi
jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
YAY
  • Exit status used as conditional for if (and while)

  • Functions, recursion, and whatnot

  • ⟶ Full programming language

  • … albeit a little weird

Exit Status#

  • An integer in the range 0-255

  • In the simplest case, a return from the program’s main function is its exit status

  • Otherwise (exiting deeper in a process’s call chain), see man -s 3 exit

Exit Status: 0 is “OK”#

#include <stdlib.h>

int main()
{
    exit(0);                                           // <-- 0 is "OK" 
    // return 0;                                       // <-- same as exit(0)
}
  • In the sunny case, an exit status of zero is returned.

  • The truth value of zero is true, paradoxically. This makes sense though: there is only one sunny case, but many opportunities to get into trouble.

$ ./exit-ok
$ echo $?
0
  • Or, programmatically …

$ if ./exit-ok; then echo YAY; else echo NOPE; fi
YAY

Exit Status: != 0 is “Not OK”#

#include <stdlib.h>

int main()
{
    exit(42);                                          // <-- != 0 is "Not OK"
}
  • In any error case, an exit status of non-zero is returned.

  • The truth value of non-zero is false. Again, this makes sense because there are possibly many things why a program might fail.

$ ./exit-nok
$ echo $?
42
  • Or, programmatically …

$ if ./exit-nok; then echo YAY; else echo NOPE; fi
NOPE