2

Here I am developing an application that can exit for various custom reasons. What is the best range for a custom exit code (like 150-200)?

I know this opinion-based but still i wanted to know the different perspective of multiple users.

Kindly share your opinions, Thanks.

1
  • 1
    exit codes are limited to the range 0-255, with 0 by convention meaning success.
    – icarus
    Aug 13, 2020 at 5:48

2 Answers 2

7

The advanced bash scripting guide says:

exit codes 1 - 2, 126 - 165, and 255 [1] have special meanings, and should therefore be avoided for user-specified exit parameters.

and

Out of range exit values can result in unexpected exit codes. An exit value greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809 % 256 = 225).

If you are making something that could be turned into a service, it's good to avoid conflicts with (or reuse meaning from) systemd's exit codes which defines code 2-7,200-242. This link also references BSD codes 64-78.

Therefore if you are not re-using a definition from one of these standards, I'd suggest codes

  • 8-63,
  • 79-125,
  • 166-199, or
  • 243-255
1
  • The POSIX standard does not say anything about avoiding any particular codes for user-specified exit parameters. It does specify that they're in the range 0 to 255, that 0 means success, and that termination due to a signal is reported in the shell by setting the special parameter $? to signal_number+128. (See jntrnr.com/exit-codes which actually quotes the standard. 255 is not a generic "out of range" code; rather it's the mathematical result of bit-masking (-1)&255. Oct 16, 2023 at 7:34
1

Exit status is stored in a 1 byte field, so it can take any value from 0 to 255. 0 is, traditionally, the value for "success".

Not the answer you're looking for? Browse other questions tagged .