What is the valid range for program return value in Linux/bash?

ysap picture ysap · Nov 10, 2011 · Viewed 7.9k times · Source

I have a C program which returns an integer value. I was surprised to find out that when examining the return value from the shell prompt I get the value modulo 256.

/* prog.c */
int main(...) { return 257; }

--

> ./prog.e
> echo $?  
1
  • Why don't I see the whole integer?
  • Where is this behavior documented?
  • How can I get the whole 32-bit value to the shell?

Answer

Doug Moscrop picture Doug Moscrop · Nov 10, 2011

When a program exits, it can return to the parent process a small amount of information about the cause of termination, using the exit status. This is a value between 0 and 255 that the exiting process passes as an argument to exit.

http://www.gnu.org/s/hello/manual/libc/Exit-Status.html

alternatively:

http://en.wikipedia.org/wiki/Exit_status

came from "posix return codes" and "c return codes" respective Google searches.