How To: Using return (exit status) codes in Unix shells

There are so many topics which I have never addressed on Unix Tutorial pages, I just can’t wait to share them with you! One of such topics is the fundamental principle of running commands in Unix. Every command returns a numeric code when it finishes, this code can help you confirm whether the execution was a success or not.

Return codes for Unix commands

Although most of the commands in Unix are very informative, you usually get information in a form of a text messages – these messages show you the output data and sometimes the accompanying execution status.  While this is very useful for a system administrator when working on something, such a way of interaction would be incomplete without a way to easily script similar interactions in Unix shells.

This is where return codes come in really handy! Every command you run, every expression you allow your Unix shell evaluate, return a status code. This is the code known to your Unix shell but not shown on the screen. It’s usually a definitive sign of what the outcome of running a command was. It usually matches the messages shown by a command, but provides a more strict verdict on the execution.

The most common way status codes are used in a black and white approach: if the return code is zero (0), this means there were no problems with execution – the command ran successfully and was able to obtain the information requested or complete the action. If the code is any different number, this means some kind of issue – usually looking at the man page for the command will shed some light on this. The approach is to verify whether the return code is 0 or not and to act accordingly.

Return codes, exit codes, error codes

Really, all these names refer to the same value returned by a command, but show how it’s seen as a different value added to various system administration tasks. There is only one value returned

Examples of return codes for common Unix commands