How to detect if a script is being sourced

I have a script where I do not want it to call exit if it’s being sourced.

I thought of checking if $0 == bash but this has problems if the script is sourced from another script, or if the user sources it from a different shell like ksh.

Is there a reliable way of detecting if a script is being sourced?

21 Answers
21

Leave a Comment