title: What is the bash equivalent to Python's `if __name__ == '__main__'`?
body: <p><strong>In Bash, I would like to be able to both source a script and execute the file. What is Bash's equivalent to Python's <code>if __name__ == '__main__'</code>?</strong></p>
  
  <p>I didn't find a readily available question/solution about this topic on Stackoverflow (I suspect I am asking in such a way that doesn't match an existing question/answer, but this is the most obvious way I can think to phrase the question because of my Python experience).</p>
  
  <hr>
  
  <p>p.s. regarding the <a href="https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced">possible duplicate question</a> (if I had more time, I would have written a shorter response):</p>
  
  <p>The <a href="https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced">linked to question</a> asks "How to detect if a script is being sourced" but this question asks "how do you create a bash script that can be both sourced AND run as a script?". The answer to this question may use some aspects of the previous question but has additional requirements/questions as follows: </p>
  
  <ul>
  <li>Once you detect the script is being sourced what is the best way to not run the script (and avoid unintended <a href="http://How%20to%20detect%20if%20a%20script%20is%20being%20sourced" rel="noreferrer">side-effects</a> (other than importing the functions of interest) like adding/removing/modifying the environment/variables) </li>
  <li>Once you detect the script is being run instead of sourced what is the canonical way of implementing your script (put it in a function? or maybe just put it after the if statement? if you put it after the if statement will it have side-affects? </li>
  <li>most google searches I found on Bash do not cover this topic (a bash script that can be both sourced and executed) what is the canonical way to implement this? is the topic not covered because it is discouraged or bad to do? are there gotchas?</li>
  </ul>