Skip to main content
2 of 2
added 470 characters in body
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

An exit statement on the last line of a script is not necessary unless you want the script to terminate with some specific exit status, in which case you may want to use e.g. exit 0 to signal "success" or exit 1 to signal "failure", to the caller.

Without an argument, exit will terminate the script with whatever value $? is, i.e., with the exit status of whatever was the last command to execute (echo in your example). This also happens by default when the script runs to its end. An explicit exit or exit "$?" is therefore never needed.

This also applies to return in shell functions and in "dot-scripts" (scripts executed via . or the non-standard source). That is, the exit status of a shell function call or a dot-script is the exit status of the last executed command. An explicit return without argument is never necessary at the end of a shell function or dot-script. You would only use return to exit the function before its end and/or to exit the function with an explicit exit status.

Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k