What is the min and max values of exit codes in Linux?
Clash Royale CLAN TAG#URR8PPP
up vote
30
down vote
favorite
What is the min and max values of the following exit codes in Linux:
- The exit code returned from a binary executable (for example: a C
program). - The exit code returned from a bash script (when calling
exit
). - The exit code returned from a function (when calling
return
). I
think this is between0
and255
.
linux executable function return-status exit-code
add a comment |Â
up vote
30
down vote
favorite
What is the min and max values of the following exit codes in Linux:
- The exit code returned from a binary executable (for example: a C
program). - The exit code returned from a bash script (when calling
exit
). - The exit code returned from a function (when calling
return
). I
think this is between0
and255
.
linux executable function return-status exit-code
For part 3, do you mean returning from a shell function? That may depend on the shell, but I note that Bash's manual says "Exit statuses fall between 0 and 255" and "Exit statuses from shell builtins and compound commands are also limited to this range."return
is, of course, a shell builtin.
â Toby Speight
Jan 24 at 13:33
Related (has answers to most of your questions): Default exit code when process is terminated?
â Stéphane Chazelas
Jan 24 at 15:32
@TobySpeight, that's a limitation of thebash
shell. Some other shells likezsh
can return any signed 32 bit value like forexit
. Some likerc
ores
can return data of any of the types they support (scalar or list). See the linked Q&A for details.
â Stéphane Chazelas
Jan 24 at 15:36
add a comment |Â
up vote
30
down vote
favorite
up vote
30
down vote
favorite
What is the min and max values of the following exit codes in Linux:
- The exit code returned from a binary executable (for example: a C
program). - The exit code returned from a bash script (when calling
exit
). - The exit code returned from a function (when calling
return
). I
think this is between0
and255
.
linux executable function return-status exit-code
What is the min and max values of the following exit codes in Linux:
- The exit code returned from a binary executable (for example: a C
program). - The exit code returned from a bash script (when calling
exit
). - The exit code returned from a function (when calling
return
). I
think this is between0
and255
.
linux executable function return-status exit-code
edited Jan 22 at 9:57
Tomasz
8,04052560
8,04052560
asked Jan 22 at 7:59
user271801
15423
15423
For part 3, do you mean returning from a shell function? That may depend on the shell, but I note that Bash's manual says "Exit statuses fall between 0 and 255" and "Exit statuses from shell builtins and compound commands are also limited to this range."return
is, of course, a shell builtin.
â Toby Speight
Jan 24 at 13:33
Related (has answers to most of your questions): Default exit code when process is terminated?
â Stéphane Chazelas
Jan 24 at 15:32
@TobySpeight, that's a limitation of thebash
shell. Some other shells likezsh
can return any signed 32 bit value like forexit
. Some likerc
ores
can return data of any of the types they support (scalar or list). See the linked Q&A for details.
â Stéphane Chazelas
Jan 24 at 15:36
add a comment |Â
For part 3, do you mean returning from a shell function? That may depend on the shell, but I note that Bash's manual says "Exit statuses fall between 0 and 255" and "Exit statuses from shell builtins and compound commands are also limited to this range."return
is, of course, a shell builtin.
â Toby Speight
Jan 24 at 13:33
Related (has answers to most of your questions): Default exit code when process is terminated?
â Stéphane Chazelas
Jan 24 at 15:32
@TobySpeight, that's a limitation of thebash
shell. Some other shells likezsh
can return any signed 32 bit value like forexit
. Some likerc
ores
can return data of any of the types they support (scalar or list). See the linked Q&A for details.
â Stéphane Chazelas
Jan 24 at 15:36
For part 3, do you mean returning from a shell function? That may depend on the shell, but I note that Bash's manual says "Exit statuses fall between 0 and 255" and "Exit statuses from shell builtins and compound commands are also limited to this range."
return
is, of course, a shell builtin.â Toby Speight
Jan 24 at 13:33
For part 3, do you mean returning from a shell function? That may depend on the shell, but I note that Bash's manual says "Exit statuses fall between 0 and 255" and "Exit statuses from shell builtins and compound commands are also limited to this range."
return
is, of course, a shell builtin.â Toby Speight
Jan 24 at 13:33
Related (has answers to most of your questions): Default exit code when process is terminated?
â Stéphane Chazelas
Jan 24 at 15:32
Related (has answers to most of your questions): Default exit code when process is terminated?
â Stéphane Chazelas
Jan 24 at 15:32
@TobySpeight, that's a limitation of the
bash
shell. Some other shells like zsh
can return any signed 32 bit value like for exit
. Some like rc
or es
can return data of any of the types they support (scalar or list). See the linked Q&A for details.â Stéphane Chazelas
Jan 24 at 15:36
@TobySpeight, that's a limitation of the
bash
shell. Some other shells like zsh
can return any signed 32 bit value like for exit
. Some like rc
or es
can return data of any of the types they support (scalar or list). See the linked Q&A for details.â Stéphane Chazelas
Jan 24 at 15:36
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
62
down vote
The number passed to the _exit()
/exit_group()
system call (sometimes referred as the exit code to avoid the ambiguity with exit status which is also referring to an encoding of either the exit code or signal number and additional info depending on whether the process was killed or exited normally) is of type int
, so on Unix-like systems like Linux, typically a 32bit integer with values from -2147483648 (-231) to 2147483647 (231-1).
However, on all systems, when the parent process (or the child subreaper or init
if the parent died) uses the wait()
, waitpid()
, wait3()
, wait4()
system calls to retrieve it, only the lower 8 bits of it are available (values 0 to 255 (28-1)).
When using the waitid()
API (or a signal handler on SIGCHLD), on most systems (and as POSIX now more clearly requires in the 2016 edition of the standard (see _exit()
specification)), the full number is available (in the si_status
field of the returned structure). That is not the case on Linux yet though which also truncates the number to 8 bits with the waitid()
API, though that's likely to change in the future.
Generally, you'd want to only use values 0 (generally meaning success) to 125 only, as many shells use values above 128 in their $?
representation of the exit status to encode the signal number of a process being killed and 126 and 127 for special conditions.
You may want to use 126 to 255 on exit()
to mean the same thing as they do for the shell's $?
(like when a script does ret=$?; ...; exit "$ret"
). Using values outside 0 -> 255 is generally not useful. You'd generally only do that if you know the parent will use the waitid()
API on systems that don't truncate and you happen to have a need for the 32bit range of values. Note that if you do a exit(2048)
for instance, that will be seen as success by parents using the traditional wait*()
APIs.
More info at:
- Default exit code when process is terminated?
That Q&A should hopefully answer most of your other questions and clarify what is meant by exit status. I'll add a few more things:
A process cannot terminate unless it's killed or calls the _exit()
/exit_group()
system calls. When you return from main()
in C
, the libc calls that system call with the return value.
Most languages have a exit()
function that wraps that system call, and the value they take, if any is generally passed as is to the system call. (note that those generally do more things like the clean-up done by C's exit()
function that flushes the stdio buffers, runs the atexit()
hooks...)
That's the case of at least:
$ strace -e exit_group awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group mawk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group busybox awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ echo | strace -e exit_group sed 'Q1234'
exit_group(1234) = ?
$ strace -e exit_group perl -e 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group python -c 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group expect -c 'exit 1234'
exit_group(1234) = ?
$ strace -e exit_group php -r 'exit(1234);'
exit_group(1234) = ?
$ strace -e exit_group zsh -c 'exit 1234'
exit_group(1234)
You occasionaly see some that complain when you use a value outside of 0-255:
$ echo 'm4exit(1234)' | strace -e exit_group m4
m4:stdin:1: exit status out of range: `1234'
exit_group(1) = ?
Some shells complain when you use a negative value:
$ strace -e exit_group dash -c 'exit -1234'
dash: 1: exit: Illegal number: -1234
exit_group(2) = ?
$ strace -e exit_group yash -c 'exit -- -1234'
exit: `-1234' is not a valid integer
exit_group(2) = ?
POSIX leaves the behaviour undefined if the value passed to the exit
special builtin is outside 0->255.
Some shells show some unexpected behaviours if you do:
bash
(andmksh
but notpdksh
on which it is based) takes upon itself to truncate the value to 8 bits:$ strace -e exit_group bash -c 'exit 1234'
exit_group(210) = ?So in those shells, if you do want to exit with a value outside of 0-255, you have to do something like:
exec zsh -c 'exit -- -12345'
exec perl -e 'exit(-12345)'That is execute another command in the same process that can call the system call with the value you want.
as mentioned at that other Q&A,
ksh93
has the weirdest behaviour for exit values from 257 to 256+max_signal_number where instead of callingexit_group()
, it kills itself with the corresponding signalù.$ ksh -c 'exit "$((256 + $(kill -l STOP)))"'
zsh: suspended (signal) ksh -c 'exit "$((256 + $(kill -l STOP)))"'and otherwise truncates the number like
bash
/mksh
.
ù That's likely to change in the next version though. Now that the development of ksh93
has been taken over as a community effort outside of AT&T, that behaviour, even though encouraged somehow by POSIX, is being reverted
1
Do you know if there's any discussion on implementing the full exit code insi_status
for Linux?
â Ruslan
Jan 22 at 10:45
2
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
@Voo, those tiny microcontrollers can't run Linux. While C requiresint
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.
â Stéphane Chazelas
Jan 24 at 10:06
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
add a comment |Â
up vote
10
down vote
The minimum is 0
, and that's considered the success value. All the other ones are failure. The maximum is 255
also known as -1
.
These rules apply for both scripts and other executables, as well as shell functions.
Bigger values result to modulo 256.
2
To be precise, in some Bourne-like shells (but notbash
or other most commonly used ones) the exit code passed to theexit
builtin is not treated as modulo-256, and instead causes an error. (For example, the commonexit -1
is actually not a portable equivalent toexit 255
in most shells). And whetherexit(-1)
at the C level is equivalent toexit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).
â mtraceur
Jan 23 at 18:35
add a comment |Â
up vote
4
down vote
This looks so simple, but oh teh woes.
The C language (and following that most other languages directly or indirectly) requires that returning from main
be equivalent to calling exit
with the same argument as the return value. This is an integer (the return type is very clearly int
), so in principle the range would be INT_MIN
to INT_MAX
.
However, POSIX states that only the lowermost 8 bits passed to exit
shall be made available to a waiting parent process, literally as if it was "status & 0xFF".
So, in practice, the exit code is a (still signed) integer of which only the lowest 8 bits are set.
The minimum will thus be -128, and the maximum 127. Hang on, that's not true. It will be 0 to 255.
But alas, of course it cannot be that simple. In practice, Linux (or rather bash) does it differently. The valid range of return codes is 0 to 255 (i.e. unsigned).
To be on the safe side in terms of avoiding confusion, it's probably a good idea to just assume that return codes are unsigned, and cast anything you get back from wait
to unsigned. That way it's consistent with what you see in a shell. Since the topmost bits (including the most significant one) are cleared out, that's not even "wrong" because although technically signed, the actual values are always unsigned (since the sign bit is never set).
It also helps avoiding the common error of comparing an exit code to -1
, which for some strange reason never seems to appear even when a program exits with -1
(well, guess why!).
About your last point, returning from a function, if this function happens to be main
, then see above. Otherwise, it depends on what the function's return type is, it could in principle be anything (including void
).
add a comment |Â
up vote
2
down vote
- The exit code returned from a binary executable (for example: a C program).
- The exit code returned from a bash script (when calling exit).
Exit codes from any process -- whether it's a binary executable, a shell script, or anything else -- range from 0 to 255. It's possible to pass a larger value to exit()
, but only the lower 8 bits of the status are made available to other processes through wait()
.
- The exit code returned from a function (when calling return). I think this is between 0 and 255.
A C function can be declared as returning almost any type. The limits of its return value are determined entirely by that type: for instance, -128 to 127 for a function returning signed char
, or 0 to 4.2 billion for a function returning unsigned int
, or any floating-point number up to and including inf
for a function returning double
. And that isn't counting the non-numeric types, like void *
or a struct
...
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
62
down vote
The number passed to the _exit()
/exit_group()
system call (sometimes referred as the exit code to avoid the ambiguity with exit status which is also referring to an encoding of either the exit code or signal number and additional info depending on whether the process was killed or exited normally) is of type int
, so on Unix-like systems like Linux, typically a 32bit integer with values from -2147483648 (-231) to 2147483647 (231-1).
However, on all systems, when the parent process (or the child subreaper or init
if the parent died) uses the wait()
, waitpid()
, wait3()
, wait4()
system calls to retrieve it, only the lower 8 bits of it are available (values 0 to 255 (28-1)).
When using the waitid()
API (or a signal handler on SIGCHLD), on most systems (and as POSIX now more clearly requires in the 2016 edition of the standard (see _exit()
specification)), the full number is available (in the si_status
field of the returned structure). That is not the case on Linux yet though which also truncates the number to 8 bits with the waitid()
API, though that's likely to change in the future.
Generally, you'd want to only use values 0 (generally meaning success) to 125 only, as many shells use values above 128 in their $?
representation of the exit status to encode the signal number of a process being killed and 126 and 127 for special conditions.
You may want to use 126 to 255 on exit()
to mean the same thing as they do for the shell's $?
(like when a script does ret=$?; ...; exit "$ret"
). Using values outside 0 -> 255 is generally not useful. You'd generally only do that if you know the parent will use the waitid()
API on systems that don't truncate and you happen to have a need for the 32bit range of values. Note that if you do a exit(2048)
for instance, that will be seen as success by parents using the traditional wait*()
APIs.
More info at:
- Default exit code when process is terminated?
That Q&A should hopefully answer most of your other questions and clarify what is meant by exit status. I'll add a few more things:
A process cannot terminate unless it's killed or calls the _exit()
/exit_group()
system calls. When you return from main()
in C
, the libc calls that system call with the return value.
Most languages have a exit()
function that wraps that system call, and the value they take, if any is generally passed as is to the system call. (note that those generally do more things like the clean-up done by C's exit()
function that flushes the stdio buffers, runs the atexit()
hooks...)
That's the case of at least:
$ strace -e exit_group awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group mawk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group busybox awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ echo | strace -e exit_group sed 'Q1234'
exit_group(1234) = ?
$ strace -e exit_group perl -e 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group python -c 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group expect -c 'exit 1234'
exit_group(1234) = ?
$ strace -e exit_group php -r 'exit(1234);'
exit_group(1234) = ?
$ strace -e exit_group zsh -c 'exit 1234'
exit_group(1234)
You occasionaly see some that complain when you use a value outside of 0-255:
$ echo 'm4exit(1234)' | strace -e exit_group m4
m4:stdin:1: exit status out of range: `1234'
exit_group(1) = ?
Some shells complain when you use a negative value:
$ strace -e exit_group dash -c 'exit -1234'
dash: 1: exit: Illegal number: -1234
exit_group(2) = ?
$ strace -e exit_group yash -c 'exit -- -1234'
exit: `-1234' is not a valid integer
exit_group(2) = ?
POSIX leaves the behaviour undefined if the value passed to the exit
special builtin is outside 0->255.
Some shells show some unexpected behaviours if you do:
bash
(andmksh
but notpdksh
on which it is based) takes upon itself to truncate the value to 8 bits:$ strace -e exit_group bash -c 'exit 1234'
exit_group(210) = ?So in those shells, if you do want to exit with a value outside of 0-255, you have to do something like:
exec zsh -c 'exit -- -12345'
exec perl -e 'exit(-12345)'That is execute another command in the same process that can call the system call with the value you want.
as mentioned at that other Q&A,
ksh93
has the weirdest behaviour for exit values from 257 to 256+max_signal_number where instead of callingexit_group()
, it kills itself with the corresponding signalù.$ ksh -c 'exit "$((256 + $(kill -l STOP)))"'
zsh: suspended (signal) ksh -c 'exit "$((256 + $(kill -l STOP)))"'and otherwise truncates the number like
bash
/mksh
.
ù That's likely to change in the next version though. Now that the development of ksh93
has been taken over as a community effort outside of AT&T, that behaviour, even though encouraged somehow by POSIX, is being reverted
1
Do you know if there's any discussion on implementing the full exit code insi_status
for Linux?
â Ruslan
Jan 22 at 10:45
2
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
@Voo, those tiny microcontrollers can't run Linux. While C requiresint
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.
â Stéphane Chazelas
Jan 24 at 10:06
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
add a comment |Â
up vote
62
down vote
The number passed to the _exit()
/exit_group()
system call (sometimes referred as the exit code to avoid the ambiguity with exit status which is also referring to an encoding of either the exit code or signal number and additional info depending on whether the process was killed or exited normally) is of type int
, so on Unix-like systems like Linux, typically a 32bit integer with values from -2147483648 (-231) to 2147483647 (231-1).
However, on all systems, when the parent process (or the child subreaper or init
if the parent died) uses the wait()
, waitpid()
, wait3()
, wait4()
system calls to retrieve it, only the lower 8 bits of it are available (values 0 to 255 (28-1)).
When using the waitid()
API (or a signal handler on SIGCHLD), on most systems (and as POSIX now more clearly requires in the 2016 edition of the standard (see _exit()
specification)), the full number is available (in the si_status
field of the returned structure). That is not the case on Linux yet though which also truncates the number to 8 bits with the waitid()
API, though that's likely to change in the future.
Generally, you'd want to only use values 0 (generally meaning success) to 125 only, as many shells use values above 128 in their $?
representation of the exit status to encode the signal number of a process being killed and 126 and 127 for special conditions.
You may want to use 126 to 255 on exit()
to mean the same thing as they do for the shell's $?
(like when a script does ret=$?; ...; exit "$ret"
). Using values outside 0 -> 255 is generally not useful. You'd generally only do that if you know the parent will use the waitid()
API on systems that don't truncate and you happen to have a need for the 32bit range of values. Note that if you do a exit(2048)
for instance, that will be seen as success by parents using the traditional wait*()
APIs.
More info at:
- Default exit code when process is terminated?
That Q&A should hopefully answer most of your other questions and clarify what is meant by exit status. I'll add a few more things:
A process cannot terminate unless it's killed or calls the _exit()
/exit_group()
system calls. When you return from main()
in C
, the libc calls that system call with the return value.
Most languages have a exit()
function that wraps that system call, and the value they take, if any is generally passed as is to the system call. (note that those generally do more things like the clean-up done by C's exit()
function that flushes the stdio buffers, runs the atexit()
hooks...)
That's the case of at least:
$ strace -e exit_group awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group mawk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group busybox awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ echo | strace -e exit_group sed 'Q1234'
exit_group(1234) = ?
$ strace -e exit_group perl -e 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group python -c 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group expect -c 'exit 1234'
exit_group(1234) = ?
$ strace -e exit_group php -r 'exit(1234);'
exit_group(1234) = ?
$ strace -e exit_group zsh -c 'exit 1234'
exit_group(1234)
You occasionaly see some that complain when you use a value outside of 0-255:
$ echo 'm4exit(1234)' | strace -e exit_group m4
m4:stdin:1: exit status out of range: `1234'
exit_group(1) = ?
Some shells complain when you use a negative value:
$ strace -e exit_group dash -c 'exit -1234'
dash: 1: exit: Illegal number: -1234
exit_group(2) = ?
$ strace -e exit_group yash -c 'exit -- -1234'
exit: `-1234' is not a valid integer
exit_group(2) = ?
POSIX leaves the behaviour undefined if the value passed to the exit
special builtin is outside 0->255.
Some shells show some unexpected behaviours if you do:
bash
(andmksh
but notpdksh
on which it is based) takes upon itself to truncate the value to 8 bits:$ strace -e exit_group bash -c 'exit 1234'
exit_group(210) = ?So in those shells, if you do want to exit with a value outside of 0-255, you have to do something like:
exec zsh -c 'exit -- -12345'
exec perl -e 'exit(-12345)'That is execute another command in the same process that can call the system call with the value you want.
as mentioned at that other Q&A,
ksh93
has the weirdest behaviour for exit values from 257 to 256+max_signal_number where instead of callingexit_group()
, it kills itself with the corresponding signalù.$ ksh -c 'exit "$((256 + $(kill -l STOP)))"'
zsh: suspended (signal) ksh -c 'exit "$((256 + $(kill -l STOP)))"'and otherwise truncates the number like
bash
/mksh
.
ù That's likely to change in the next version though. Now that the development of ksh93
has been taken over as a community effort outside of AT&T, that behaviour, even though encouraged somehow by POSIX, is being reverted
1
Do you know if there's any discussion on implementing the full exit code insi_status
for Linux?
â Ruslan
Jan 22 at 10:45
2
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
@Voo, those tiny microcontrollers can't run Linux. While C requiresint
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.
â Stéphane Chazelas
Jan 24 at 10:06
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
add a comment |Â
up vote
62
down vote
up vote
62
down vote
The number passed to the _exit()
/exit_group()
system call (sometimes referred as the exit code to avoid the ambiguity with exit status which is also referring to an encoding of either the exit code or signal number and additional info depending on whether the process was killed or exited normally) is of type int
, so on Unix-like systems like Linux, typically a 32bit integer with values from -2147483648 (-231) to 2147483647 (231-1).
However, on all systems, when the parent process (or the child subreaper or init
if the parent died) uses the wait()
, waitpid()
, wait3()
, wait4()
system calls to retrieve it, only the lower 8 bits of it are available (values 0 to 255 (28-1)).
When using the waitid()
API (or a signal handler on SIGCHLD), on most systems (and as POSIX now more clearly requires in the 2016 edition of the standard (see _exit()
specification)), the full number is available (in the si_status
field of the returned structure). That is not the case on Linux yet though which also truncates the number to 8 bits with the waitid()
API, though that's likely to change in the future.
Generally, you'd want to only use values 0 (generally meaning success) to 125 only, as many shells use values above 128 in their $?
representation of the exit status to encode the signal number of a process being killed and 126 and 127 for special conditions.
You may want to use 126 to 255 on exit()
to mean the same thing as they do for the shell's $?
(like when a script does ret=$?; ...; exit "$ret"
). Using values outside 0 -> 255 is generally not useful. You'd generally only do that if you know the parent will use the waitid()
API on systems that don't truncate and you happen to have a need for the 32bit range of values. Note that if you do a exit(2048)
for instance, that will be seen as success by parents using the traditional wait*()
APIs.
More info at:
- Default exit code when process is terminated?
That Q&A should hopefully answer most of your other questions and clarify what is meant by exit status. I'll add a few more things:
A process cannot terminate unless it's killed or calls the _exit()
/exit_group()
system calls. When you return from main()
in C
, the libc calls that system call with the return value.
Most languages have a exit()
function that wraps that system call, and the value they take, if any is generally passed as is to the system call. (note that those generally do more things like the clean-up done by C's exit()
function that flushes the stdio buffers, runs the atexit()
hooks...)
That's the case of at least:
$ strace -e exit_group awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group mawk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group busybox awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ echo | strace -e exit_group sed 'Q1234'
exit_group(1234) = ?
$ strace -e exit_group perl -e 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group python -c 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group expect -c 'exit 1234'
exit_group(1234) = ?
$ strace -e exit_group php -r 'exit(1234);'
exit_group(1234) = ?
$ strace -e exit_group zsh -c 'exit 1234'
exit_group(1234)
You occasionaly see some that complain when you use a value outside of 0-255:
$ echo 'm4exit(1234)' | strace -e exit_group m4
m4:stdin:1: exit status out of range: `1234'
exit_group(1) = ?
Some shells complain when you use a negative value:
$ strace -e exit_group dash -c 'exit -1234'
dash: 1: exit: Illegal number: -1234
exit_group(2) = ?
$ strace -e exit_group yash -c 'exit -- -1234'
exit: `-1234' is not a valid integer
exit_group(2) = ?
POSIX leaves the behaviour undefined if the value passed to the exit
special builtin is outside 0->255.
Some shells show some unexpected behaviours if you do:
bash
(andmksh
but notpdksh
on which it is based) takes upon itself to truncate the value to 8 bits:$ strace -e exit_group bash -c 'exit 1234'
exit_group(210) = ?So in those shells, if you do want to exit with a value outside of 0-255, you have to do something like:
exec zsh -c 'exit -- -12345'
exec perl -e 'exit(-12345)'That is execute another command in the same process that can call the system call with the value you want.
as mentioned at that other Q&A,
ksh93
has the weirdest behaviour for exit values from 257 to 256+max_signal_number where instead of callingexit_group()
, it kills itself with the corresponding signalù.$ ksh -c 'exit "$((256 + $(kill -l STOP)))"'
zsh: suspended (signal) ksh -c 'exit "$((256 + $(kill -l STOP)))"'and otherwise truncates the number like
bash
/mksh
.
ù That's likely to change in the next version though. Now that the development of ksh93
has been taken over as a community effort outside of AT&T, that behaviour, even though encouraged somehow by POSIX, is being reverted
The number passed to the _exit()
/exit_group()
system call (sometimes referred as the exit code to avoid the ambiguity with exit status which is also referring to an encoding of either the exit code or signal number and additional info depending on whether the process was killed or exited normally) is of type int
, so on Unix-like systems like Linux, typically a 32bit integer with values from -2147483648 (-231) to 2147483647 (231-1).
However, on all systems, when the parent process (or the child subreaper or init
if the parent died) uses the wait()
, waitpid()
, wait3()
, wait4()
system calls to retrieve it, only the lower 8 bits of it are available (values 0 to 255 (28-1)).
When using the waitid()
API (or a signal handler on SIGCHLD), on most systems (and as POSIX now more clearly requires in the 2016 edition of the standard (see _exit()
specification)), the full number is available (in the si_status
field of the returned structure). That is not the case on Linux yet though which also truncates the number to 8 bits with the waitid()
API, though that's likely to change in the future.
Generally, you'd want to only use values 0 (generally meaning success) to 125 only, as many shells use values above 128 in their $?
representation of the exit status to encode the signal number of a process being killed and 126 and 127 for special conditions.
You may want to use 126 to 255 on exit()
to mean the same thing as they do for the shell's $?
(like when a script does ret=$?; ...; exit "$ret"
). Using values outside 0 -> 255 is generally not useful. You'd generally only do that if you know the parent will use the waitid()
API on systems that don't truncate and you happen to have a need for the 32bit range of values. Note that if you do a exit(2048)
for instance, that will be seen as success by parents using the traditional wait*()
APIs.
More info at:
- Default exit code when process is terminated?
That Q&A should hopefully answer most of your other questions and clarify what is meant by exit status. I'll add a few more things:
A process cannot terminate unless it's killed or calls the _exit()
/exit_group()
system calls. When you return from main()
in C
, the libc calls that system call with the return value.
Most languages have a exit()
function that wraps that system call, and the value they take, if any is generally passed as is to the system call. (note that those generally do more things like the clean-up done by C's exit()
function that flushes the stdio buffers, runs the atexit()
hooks...)
That's the case of at least:
$ strace -e exit_group awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group mawk 'BEGINexit(1234)'
exit_group(1234) = ?
$ strace -e exit_group busybox awk 'BEGINexit(1234)'
exit_group(1234) = ?
$ echo | strace -e exit_group sed 'Q1234'
exit_group(1234) = ?
$ strace -e exit_group perl -e 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group python -c 'exit(1234)'
exit_group(1234) = ?
$ strace -e exit_group expect -c 'exit 1234'
exit_group(1234) = ?
$ strace -e exit_group php -r 'exit(1234);'
exit_group(1234) = ?
$ strace -e exit_group zsh -c 'exit 1234'
exit_group(1234)
You occasionaly see some that complain when you use a value outside of 0-255:
$ echo 'm4exit(1234)' | strace -e exit_group m4
m4:stdin:1: exit status out of range: `1234'
exit_group(1) = ?
Some shells complain when you use a negative value:
$ strace -e exit_group dash -c 'exit -1234'
dash: 1: exit: Illegal number: -1234
exit_group(2) = ?
$ strace -e exit_group yash -c 'exit -- -1234'
exit: `-1234' is not a valid integer
exit_group(2) = ?
POSIX leaves the behaviour undefined if the value passed to the exit
special builtin is outside 0->255.
Some shells show some unexpected behaviours if you do:
bash
(andmksh
but notpdksh
on which it is based) takes upon itself to truncate the value to 8 bits:$ strace -e exit_group bash -c 'exit 1234'
exit_group(210) = ?So in those shells, if you do want to exit with a value outside of 0-255, you have to do something like:
exec zsh -c 'exit -- -12345'
exec perl -e 'exit(-12345)'That is execute another command in the same process that can call the system call with the value you want.
as mentioned at that other Q&A,
ksh93
has the weirdest behaviour for exit values from 257 to 256+max_signal_number where instead of callingexit_group()
, it kills itself with the corresponding signalù.$ ksh -c 'exit "$((256 + $(kill -l STOP)))"'
zsh: suspended (signal) ksh -c 'exit "$((256 + $(kill -l STOP)))"'and otherwise truncates the number like
bash
/mksh
.
ù That's likely to change in the next version though. Now that the development of ksh93
has been taken over as a community effort outside of AT&T, that behaviour, even though encouraged somehow by POSIX, is being reverted
edited Jan 24 at 11:13
answered Jan 22 at 9:35
Stéphane Chazelas
281k53518849
281k53518849
1
Do you know if there's any discussion on implementing the full exit code insi_status
for Linux?
â Ruslan
Jan 22 at 10:45
2
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
@Voo, those tiny microcontrollers can't run Linux. While C requiresint
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.
â Stéphane Chazelas
Jan 24 at 10:06
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
add a comment |Â
1
Do you know if there's any discussion on implementing the full exit code insi_status
for Linux?
â Ruslan
Jan 22 at 10:45
2
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
@Voo, those tiny microcontrollers can't run Linux. While C requiresint
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.
â Stéphane Chazelas
Jan 24 at 10:06
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
1
1
Do you know if there's any discussion on implementing the full exit code in
si_status
for Linux?â Ruslan
Jan 22 at 10:45
Do you know if there's any discussion on implementing the full exit code in
si_status
for Linux?â Ruslan
Jan 22 at 10:45
2
2
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
@Ruslan, not more than the austingroupbugs.net/view.php?id=594#c1318 (from Eric Blake (RedHat)) at the link I gave
â Stéphane Chazelas
Jan 22 at 10:56
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
"is of type int, so a 32bit integer". Linux really guarantees that an int will always be 32bit? Even when running on some of those tiny microcontrollers? That strikes me as really odd. POSIX certainly doesn't.
â Voo
Jan 24 at 8:10
@Voo, those tiny microcontrollers can't run Linux. While C requires
int
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.â Stéphane Chazelas
Jan 24 at 10:06
@Voo, those tiny microcontrollers can't run Linux. While C requires
int
to be at least 16 bits, POSIX more or less requires it to be at least 32 bits and programming environments to have a uint32_t. I don't know if Linux supports any programming environment where ints are anything but 32bits, I've never come across any.â Stéphane Chazelas
Jan 24 at 10:06
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
@Voo, (continued) there's nothing stopping a compiler to provide 32bit integers on your microcontroller with 16bit registers, but there are likely going to be many other things preventing this controller from running an OS like Linux, the fact that it has only a few kilobytes of memory for instance.
â Stéphane Chazelas
Jan 24 at 10:08
add a comment |Â
up vote
10
down vote
The minimum is 0
, and that's considered the success value. All the other ones are failure. The maximum is 255
also known as -1
.
These rules apply for both scripts and other executables, as well as shell functions.
Bigger values result to modulo 256.
2
To be precise, in some Bourne-like shells (but notbash
or other most commonly used ones) the exit code passed to theexit
builtin is not treated as modulo-256, and instead causes an error. (For example, the commonexit -1
is actually not a portable equivalent toexit 255
in most shells). And whetherexit(-1)
at the C level is equivalent toexit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).
â mtraceur
Jan 23 at 18:35
add a comment |Â
up vote
10
down vote
The minimum is 0
, and that's considered the success value. All the other ones are failure. The maximum is 255
also known as -1
.
These rules apply for both scripts and other executables, as well as shell functions.
Bigger values result to modulo 256.
2
To be precise, in some Bourne-like shells (but notbash
or other most commonly used ones) the exit code passed to theexit
builtin is not treated as modulo-256, and instead causes an error. (For example, the commonexit -1
is actually not a portable equivalent toexit 255
in most shells). And whetherexit(-1)
at the C level is equivalent toexit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).
â mtraceur
Jan 23 at 18:35
add a comment |Â
up vote
10
down vote
up vote
10
down vote
The minimum is 0
, and that's considered the success value. All the other ones are failure. The maximum is 255
also known as -1
.
These rules apply for both scripts and other executables, as well as shell functions.
Bigger values result to modulo 256.
The minimum is 0
, and that's considered the success value. All the other ones are failure. The maximum is 255
also known as -1
.
These rules apply for both scripts and other executables, as well as shell functions.
Bigger values result to modulo 256.
edited Jan 22 at 8:15
answered Jan 22 at 8:07
Tomasz
8,04052560
8,04052560
2
To be precise, in some Bourne-like shells (but notbash
or other most commonly used ones) the exit code passed to theexit
builtin is not treated as modulo-256, and instead causes an error. (For example, the commonexit -1
is actually not a portable equivalent toexit 255
in most shells). And whetherexit(-1)
at the C level is equivalent toexit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).
â mtraceur
Jan 23 at 18:35
add a comment |Â
2
To be precise, in some Bourne-like shells (but notbash
or other most commonly used ones) the exit code passed to theexit
builtin is not treated as modulo-256, and instead causes an error. (For example, the commonexit -1
is actually not a portable equivalent toexit 255
in most shells). And whetherexit(-1)
at the C level is equivalent toexit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).
â mtraceur
Jan 23 at 18:35
2
2
To be precise, in some Bourne-like shells (but not
bash
or other most commonly used ones) the exit code passed to the exit
builtin is not treated as modulo-256, and instead causes an error. (For example, the common exit -1
is actually not a portable equivalent to exit 255
in most shells). And whether exit(-1)
at the C level is equivalent to exit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).â mtraceur
Jan 23 at 18:35
To be precise, in some Bourne-like shells (but not
bash
or other most commonly used ones) the exit code passed to the exit
builtin is not treated as modulo-256, and instead causes an error. (For example, the common exit -1
is actually not a portable equivalent to exit 255
in most shells). And whether exit(-1)
at the C level is equivalent to exit(255)
is a detail that is de-facto certain to work, but relies on implementation defined behavior (though this isn't a problem on modern systems you're likely to use in practice).â mtraceur
Jan 23 at 18:35
add a comment |Â
up vote
4
down vote
This looks so simple, but oh teh woes.
The C language (and following that most other languages directly or indirectly) requires that returning from main
be equivalent to calling exit
with the same argument as the return value. This is an integer (the return type is very clearly int
), so in principle the range would be INT_MIN
to INT_MAX
.
However, POSIX states that only the lowermost 8 bits passed to exit
shall be made available to a waiting parent process, literally as if it was "status & 0xFF".
So, in practice, the exit code is a (still signed) integer of which only the lowest 8 bits are set.
The minimum will thus be -128, and the maximum 127. Hang on, that's not true. It will be 0 to 255.
But alas, of course it cannot be that simple. In practice, Linux (or rather bash) does it differently. The valid range of return codes is 0 to 255 (i.e. unsigned).
To be on the safe side in terms of avoiding confusion, it's probably a good idea to just assume that return codes are unsigned, and cast anything you get back from wait
to unsigned. That way it's consistent with what you see in a shell. Since the topmost bits (including the most significant one) are cleared out, that's not even "wrong" because although technically signed, the actual values are always unsigned (since the sign bit is never set).
It also helps avoiding the common error of comparing an exit code to -1
, which for some strange reason never seems to appear even when a program exits with -1
(well, guess why!).
About your last point, returning from a function, if this function happens to be main
, then see above. Otherwise, it depends on what the function's return type is, it could in principle be anything (including void
).
add a comment |Â
up vote
4
down vote
This looks so simple, but oh teh woes.
The C language (and following that most other languages directly or indirectly) requires that returning from main
be equivalent to calling exit
with the same argument as the return value. This is an integer (the return type is very clearly int
), so in principle the range would be INT_MIN
to INT_MAX
.
However, POSIX states that only the lowermost 8 bits passed to exit
shall be made available to a waiting parent process, literally as if it was "status & 0xFF".
So, in practice, the exit code is a (still signed) integer of which only the lowest 8 bits are set.
The minimum will thus be -128, and the maximum 127. Hang on, that's not true. It will be 0 to 255.
But alas, of course it cannot be that simple. In practice, Linux (or rather bash) does it differently. The valid range of return codes is 0 to 255 (i.e. unsigned).
To be on the safe side in terms of avoiding confusion, it's probably a good idea to just assume that return codes are unsigned, and cast anything you get back from wait
to unsigned. That way it's consistent with what you see in a shell. Since the topmost bits (including the most significant one) are cleared out, that's not even "wrong" because although technically signed, the actual values are always unsigned (since the sign bit is never set).
It also helps avoiding the common error of comparing an exit code to -1
, which for some strange reason never seems to appear even when a program exits with -1
(well, guess why!).
About your last point, returning from a function, if this function happens to be main
, then see above. Otherwise, it depends on what the function's return type is, it could in principle be anything (including void
).
add a comment |Â
up vote
4
down vote
up vote
4
down vote
This looks so simple, but oh teh woes.
The C language (and following that most other languages directly or indirectly) requires that returning from main
be equivalent to calling exit
with the same argument as the return value. This is an integer (the return type is very clearly int
), so in principle the range would be INT_MIN
to INT_MAX
.
However, POSIX states that only the lowermost 8 bits passed to exit
shall be made available to a waiting parent process, literally as if it was "status & 0xFF".
So, in practice, the exit code is a (still signed) integer of which only the lowest 8 bits are set.
The minimum will thus be -128, and the maximum 127. Hang on, that's not true. It will be 0 to 255.
But alas, of course it cannot be that simple. In practice, Linux (or rather bash) does it differently. The valid range of return codes is 0 to 255 (i.e. unsigned).
To be on the safe side in terms of avoiding confusion, it's probably a good idea to just assume that return codes are unsigned, and cast anything you get back from wait
to unsigned. That way it's consistent with what you see in a shell. Since the topmost bits (including the most significant one) are cleared out, that's not even "wrong" because although technically signed, the actual values are always unsigned (since the sign bit is never set).
It also helps avoiding the common error of comparing an exit code to -1
, which for some strange reason never seems to appear even when a program exits with -1
(well, guess why!).
About your last point, returning from a function, if this function happens to be main
, then see above. Otherwise, it depends on what the function's return type is, it could in principle be anything (including void
).
This looks so simple, but oh teh woes.
The C language (and following that most other languages directly or indirectly) requires that returning from main
be equivalent to calling exit
with the same argument as the return value. This is an integer (the return type is very clearly int
), so in principle the range would be INT_MIN
to INT_MAX
.
However, POSIX states that only the lowermost 8 bits passed to exit
shall be made available to a waiting parent process, literally as if it was "status & 0xFF".
So, in practice, the exit code is a (still signed) integer of which only the lowest 8 bits are set.
The minimum will thus be -128, and the maximum 127. Hang on, that's not true. It will be 0 to 255.
But alas, of course it cannot be that simple. In practice, Linux (or rather bash) does it differently. The valid range of return codes is 0 to 255 (i.e. unsigned).
To be on the safe side in terms of avoiding confusion, it's probably a good idea to just assume that return codes are unsigned, and cast anything you get back from wait
to unsigned. That way it's consistent with what you see in a shell. Since the topmost bits (including the most significant one) are cleared out, that's not even "wrong" because although technically signed, the actual values are always unsigned (since the sign bit is never set).
It also helps avoiding the common error of comparing an exit code to -1
, which for some strange reason never seems to appear even when a program exits with -1
(well, guess why!).
About your last point, returning from a function, if this function happens to be main
, then see above. Otherwise, it depends on what the function's return type is, it could in principle be anything (including void
).
edited Apr 12 at 3:37
Konstantin
1254
1254
answered Jan 23 at 10:46
Damon
1,37978
1,37978
add a comment |Â
add a comment |Â
up vote
2
down vote
- The exit code returned from a binary executable (for example: a C program).
- The exit code returned from a bash script (when calling exit).
Exit codes from any process -- whether it's a binary executable, a shell script, or anything else -- range from 0 to 255. It's possible to pass a larger value to exit()
, but only the lower 8 bits of the status are made available to other processes through wait()
.
- The exit code returned from a function (when calling return). I think this is between 0 and 255.
A C function can be declared as returning almost any type. The limits of its return value are determined entirely by that type: for instance, -128 to 127 for a function returning signed char
, or 0 to 4.2 billion for a function returning unsigned int
, or any floating-point number up to and including inf
for a function returning double
. And that isn't counting the non-numeric types, like void *
or a struct
...
add a comment |Â
up vote
2
down vote
- The exit code returned from a binary executable (for example: a C program).
- The exit code returned from a bash script (when calling exit).
Exit codes from any process -- whether it's a binary executable, a shell script, or anything else -- range from 0 to 255. It's possible to pass a larger value to exit()
, but only the lower 8 bits of the status are made available to other processes through wait()
.
- The exit code returned from a function (when calling return). I think this is between 0 and 255.
A C function can be declared as returning almost any type. The limits of its return value are determined entirely by that type: for instance, -128 to 127 for a function returning signed char
, or 0 to 4.2 billion for a function returning unsigned int
, or any floating-point number up to and including inf
for a function returning double
. And that isn't counting the non-numeric types, like void *
or a struct
...
add a comment |Â
up vote
2
down vote
up vote
2
down vote
- The exit code returned from a binary executable (for example: a C program).
- The exit code returned from a bash script (when calling exit).
Exit codes from any process -- whether it's a binary executable, a shell script, or anything else -- range from 0 to 255. It's possible to pass a larger value to exit()
, but only the lower 8 bits of the status are made available to other processes through wait()
.
- The exit code returned from a function (when calling return). I think this is between 0 and 255.
A C function can be declared as returning almost any type. The limits of its return value are determined entirely by that type: for instance, -128 to 127 for a function returning signed char
, or 0 to 4.2 billion for a function returning unsigned int
, or any floating-point number up to and including inf
for a function returning double
. And that isn't counting the non-numeric types, like void *
or a struct
...
- The exit code returned from a binary executable (for example: a C program).
- The exit code returned from a bash script (when calling exit).
Exit codes from any process -- whether it's a binary executable, a shell script, or anything else -- range from 0 to 255. It's possible to pass a larger value to exit()
, but only the lower 8 bits of the status are made available to other processes through wait()
.
- The exit code returned from a function (when calling return). I think this is between 0 and 255.
A C function can be declared as returning almost any type. The limits of its return value are determined entirely by that type: for instance, -128 to 127 for a function returning signed char
, or 0 to 4.2 billion for a function returning unsigned int
, or any floating-point number up to and including inf
for a function returning double
. And that isn't counting the non-numeric types, like void *
or a struct
...
answered Jan 23 at 4:17
duskwuff
22416
22416
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418784%2fwhat-is-the-min-and-max-values-of-exit-codes-in-linux%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
For part 3, do you mean returning from a shell function? That may depend on the shell, but I note that Bash's manual says "Exit statuses fall between 0 and 255" and "Exit statuses from shell builtins and compound commands are also limited to this range."
return
is, of course, a shell builtin.â Toby Speight
Jan 24 at 13:33
Related (has answers to most of your questions): Default exit code when process is terminated?
â Stéphane Chazelas
Jan 24 at 15:32
@TobySpeight, that's a limitation of the
bash
shell. Some other shells likezsh
can return any signed 32 bit value like forexit
. Some likerc
ores
can return data of any of the types they support (scalar or list). See the linked Q&A for details.â Stéphane Chazelas
Jan 24 at 15:36