Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 941
ysap Avatar asked Nov 10 '11 16:11

ysap


People also ask

What is the return value of a bash script?

When a bash function completes, its return value is the status of the last statement executed in the function, 0 for success and non-zero decimal number in the 1 - 255 range for failure. The return status can be specified by using the return keyword, and it is assigned to the variable $? .

What is return value Linux?

The return value is called exit status. This value can be used to determine whether a command completed successfully or unsuccessfully. If the command exits successfully, the exit status will be zero, otherwise it will be a nonzero value.

How do you create a range in bash?

You can iterate the sequence of numbers in bash in two ways. One is by using the seq command, and another is by specifying the range in for loop. In the seq command, the sequence starts from one, the number increments by one in each step, and print each number in each line up to the upper limit by default.

What is the return value of a shell script after successful execution?

Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.


1 Answers

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.

like image 110
Doug Moscrop Avatar answered Sep 20 '22 20:09

Doug Moscrop