Ending the Script and Setting Completion Status

When your script has done its job and is about to terminate, it is a good idea to return an exit status explicitly instead of letting the shell return the exit status of the last command executed. This is especially true if you are exiting because of an error condition, because you would like to give notice of it to any script that might be calling your script. And it is generally nice to give an exit status of zero (0) if your script exits normally so that any script that might be calling yours can check your exit status and feel confident that everything is okay. Any argument you give exit will be the exit status of your script:

cp acme /tmp/archive || { STATUS=$? echo "Error: copy failed" >&2 ...

Get Practical UNIX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.