How to raise and take care of shell scripts: error handling

All programs produce errors

At least the programs show, if they couldn't do their job properly (e. g. if a file to read does not exist). That's why you should check the successful completion of programs and functions every time, when the next steps in your script depend on this result. The exit code of the last call is stored in the variable $?. The rule is simple: The exit code 0 means success, every other value means: error. You should keep in mind the fact, that the programs do decide on their own, WHAT an error is. If you use a sed to substitute some parts of a file, it's normally not an error, if nothing was substituted. You have to check that using other methods. To learn more about these methods? Right: man command.

The scripts you write should follow the standard. They should return with an exit code 0 in case of success and with another code in case of errors. You may decide to use different exit codes for different errors. This could help other programs, which call your script, to continue depending on this exit code.But this always depends on how they interpret your return values. However, the last line of your script always should be exit 0 to show success.