To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.
Here's an example: a rewrite of the bkedit script from article 44.8. If the script can make a backup copy, the editor is run and the script returns the exit status from vi (usually 0). If something goes wrong with the copy, the script prints an error and returns an exit status of 1. Here's the script:
Here's what happens if I run it without a filename:
bkeditcp: usage: cp fn1 fn2 or cp fn1 [fn2...] dir bkedit quitting: can't make backup?
And here's what's left in the exit status variable: