Could you guys help to give the algorithm used to generate $? in shell from exit code in program? For example,
$? is 1 for exit(1);
$? is 255 for exit(-1);
So I can infer exit code from $?
$? is 1 => exit code is 1
$? is 255 => exit code is -1
For something special,
$? is 0 for exit(256);
$? is 1 for exit(257);
Could anyone give the algorithm in shell used to generate $? with exit code, so that I can know the exit code only by observing $? after executing a command.
Thanks a lot.
Edit: To answer the question below, I added this example.
----a.c----
1 #include <stdlib.h>
2 int main()
3 {
4 exit(-1);
5 }
ning@m:~/work/02_test/ctest> gcc a.c
ning@m:~/work/02_test/ctest> ./a.out
ning@m:~/work/02_test/ctest> echo $?
255
ning@m:~/work/02_test/ctest>