Reputation: 3172
On Ubuntu 14's Ruby 1.9.3, when I run the script
#!/usr/bin/env ruby
and the script
#!/usr/bin/env ruby
exit 0
they do the same thing. In particular, a subsequent echo $?
prints 0
. How universal is this behavior? Can I safely omit exit 0
from the end of a nontrivial script?
Upvotes: 1
Views: 1792
Reputation: 36100
Universally in UNIX, 0 is reserved for success and codes other than 0 - different sorts of errors.
In any normal language, unless your program throws some sort of exception or explicitly exits with a different code, you can safely assume that it exited with 0.
As per the documentation of Process#exit
:
exit(status=true)
[...]The optional parameter is used to return a status code to the invoking environment. true and FALSE of status means success and failure respectively.
The interpretation of other integer values are system dependent.
Meaning Ruby exits with success by default, whatever success means for the underlying system.
As per the documentation of exit codes from The Linux Documentation Project:
[...] A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions. [...]
Meaning you are will get 0 in a UNIX environments.
As per Microsoft's documentation on exit codes:
ERROR_SUCCESS
0 (0x0)
The operation completed successfully.
Meaning you will also get zero in Windows.
Upvotes: 4