Search code examples
bashexit

are there any benefits of ending a bash script with exit


I encountered a bash script ending with the exit line. Does it changes behavior (save scaring users who 'source' rather than calling straight with closing the terminal )?

Note that I am not particularly interested in difference between exit and return. Here I am only interested in differences between having exit without parameters in the end of a bash script (one being closing console or process which sources the script rather than calling).

Could it be to address some less known shell dialects?


Solution

  • There are generally no benefits to doing this. There are only downsides, specifically the inability to source scripts like you say.

    You can construct scenarios where it matters, such as having a sourceing script rely on it for termination on errors, or having a self-extracting archive header avoid executing its payload, but these unusual cases should not be the basis for a general guideline.