Unix History: return code octal

gdbhistory

I was thrown off guard today by gdb:

Program exited with code 0146.

gdb prints the return code in octal; looking into why I found:
http://comments.gmane.org/gmane.comp.gdb.devel/30363

But that's not a particularly satisfying answer. Some quick googling did not reveal the history, so I was hoping someone on SO might know the back story.

A somewhat related question, how would one even view the return code in octal? Perhaps older machines always printed the return code?

$ printf %o\\n $?

Is pretty awkward 🙂

Best Answer

The octal representation eases the interpretation of the exit code for small values, which are the most commonly used. Should this number, which is a byte, been printed in decimal, finding which signal interrupted a process would require a little bit of calculation while in octal, they can be read as they are:

  • a process exits with status 5, gdb displays 05 which makes no difference
  • a process exits because it got a SIGINT (Control+C), gdb displays 0202 which is easier to recognize as signal #2 than 130.

Moreover, the exit status might also be a bit mask and in such case, octal (at least when you are used to it which was more common a couple of decades ago than these days) is easier to convert mentally into bits than decimal or even hexadecimal, just like for example chmod still accept an octal number to represent file permissions: 0750 = 111 101 000 = rwx r-x ---.

Related Question