It is kind of clear that it doesn't matter if I use exit(1) or exit(42)
It actually matters a lot.
The exit code can be used by the process that launches the exiting process to know how it completed and why it failed.
The process that launches your program can inspect the value of the environment variable $?
immediately after your program completes to know if it succeeded or why it failed, if it didn't succeed.
Let's say your program downloads a file from a remote site and stores it in a local directory. It expects to use an existing local directory and it doesn't attempt to create it if it doesn't exist. It can exit, for example, with code 37
when the remote file cannot be downloaded because the remote site return 404 Not Found
, with code 62
when it cannot download the file because the network is down (or a timeout happens) and code 41
when the local directory does not exist.
A bash
script, for example, that invokes your program can check the value of the environment variable $?
immediately after your program completes. If its value is 37
(remote file is not found) it must not attempt to retry because the error is permanent. On exit code 62
(network issues) it can wait a couple of seconds and try again (the error condition is transient, it could disappear after a while). On exit code 41
(local directory not found) it can create the local directory then launch your program again (a precondition was not met).