What is a good Linux exit error code strategy? What is a good Linux exit error code strategy? unix unix

What is a good Linux exit error code strategy?


The only convention is that you return 0 for success, and something other than zero for an error. Most well-known unix programs document the various return codes that they can return, and so should you. It doesn't make a lot of sense to try to make a common list for all possible error codes that any arbitrary program could return, or else you end up with tens of thousands of them like some other OS's, and even then, it doesn't always cover the specific type of error you want to return.

So just be consistent, and be sure to document whatever scheme you decide to use.


1-127 is the available range. Anything over 127 is supposed to be "abnormal" exit - terminated by a signal.

While you're at it, consider using stdout rather than exit code. Exit code is by tradition used to indicate success, failure, and may be one other state. Rather than using exit code, try using stdout the way expr and wc use it. You can then use backtick or something similar in the caller to extract the result.


the unix manifesto states -

Exit as soon and as loud as possible on error

or something like that