Deciphering shell exit codes

I was recently debugging an issue with a shell script, and noticed that the shell was exiting with an exit code greater than 100 when it received a SIGTSTP signal:

$ cat test

#!/bin/bash
sleep 60

# Window one
$ ./test
[1]+ Stopped ./test
Home:~ matty$ echo $?
146

# Window two
$ kill -18 4667

I was curious where the exit value of 146 came from, so I did a bit of digging. It turns out that when a shell exits due to an uncaght signal, the signal number is added to 128 and that is the value that is returned. So in the case above, the exit code 146 was returned. I digs me some random shell knowledge.

One Comment

Bernd Eckenfels  on November 20th, 2008

In this case the shell does not exit, it just is stopped. Interesting that the parent shell continues in this case with an return code.

Greetings
Bernd

Leave a Comment