Determine if a script was sourced or executed
Bash only allows return
in a function or at the top level of a sourced script. We can call it in a subshell and use the exit code to determine if the script is being sourced or not.
If all we want to do is to halt execution at some point if we're sourcing the script, we can use the simpler return 0 2>/dev/null
as a short-circuit.
Example usage:
Source: stackoverflow
Last updated