Josh
Josh

Reputation: 21

Difference between errno and return code in Linux

What is the difference between errno available from <errno.h> and the return code which can be printed using echo $? in bash. Are they referring to the same code?

Upvotes: 2

Views: 1955

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60056

A process's exit code doesn't usually correspond to an errno code.

The most portable strategy is just to have 1 for failure exits (or even more portably EXIT_FAILURE as defined in stdlib.h) and 0 for success exits.

Some programs follow the bsd sysexits strategy for mapping mores specific exit conditions to exit codes.

Other programs may choose to return errno codes (relevant to the system call failure that ultimately led to a process exit) or e.g., errno+1 , and you'll usually be able to fit that into the 8 bits used by exit, but there's no system-wide-enforced exit code strategy or even an unenforced convention apart from a zero exit code meaning success and a nonzero exit code meaning (some kind of) failure.

Upvotes: 1

Related Questions