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

非 Y 不嫁゛ 提交于 2019-11-27 05:34:27

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.

The explanation is right at the top of man exit:

   The  exit() function causes normal process termination and the value of
   status & 0377 is returned to the parent (see wait(2)).

In other words, only the lowest 8 bits are propagated to the parent process.

In this respect, returning the exit code from main() is no different to passing it to exit().

Mat

The return status is explained (sort of) in the wait and related syscalls.

Basically:

WEXITSTATUS(stat_val)
If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

So it's limited to 8 bits. You can't portably get more than that. (With Linux kernel 2.6.9 and above, waitid(2) can be used to obtain the full 32 bits.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!