1

I am writing a program in C for x86 Linux. I am wanting to use my own customer exit codes to help the user with debugging problems when the program doesn't execute as expected.

As an example, suppose the program was a command line calculator and I would like to return the following exit codes and document them in the man pages;

  1. Calculation completes and prints results to screen (normal operation) - return 0
  2. Invalid number of operands supplied (syntax error basically!) - return 1
  3. Some other error - return 2
  4. Another error - return 3

I have seen a few pages like this one which make me think I can't return any exit code I like. Is there any official rules (BASH or Linux standard/guideline) I should be following here, apart from 0 == normal exit?

2
  • 2
    the big limit is that the $? (return code) can be no greater than 255. Anything else is up to you, and as long as you document the return values and the error that your flagging, there shouldn't be any problem. Good link, best to observe those restrictions. Good luck.
    – shellter
    Oct 1, 2013 at 20:20
  • 1
    Exit codes are a feature of the operating system, not any particular shell.
    – chepner
    Oct 1, 2013 at 20:22

1 Answer 1

5

You can return any integral exit code you like. The BASH scripting guide page you reference just says it could be confusing when debugging something that returns a well-known code for some other reason.

That page also mentions /usr/include/sysexits.h as an attempt to systematize exit codes for C programmers.

1
  • Thanks for the pointer to /usr/include/sysexits.h I didn't know about this!
    – Baldrick
    Oct 1, 2013 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.