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?
If your Bash version knows about the BASH_SOURCE array variable, try something like:
# man bash | less -p BASH_SOURCE
#[[ ${BASH_VERSINFO[0]} -le 2 ]] && echo 'No BASH_SOURCE array variable' && exit 1
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && echo "script ${BASH_SOURCE[0]} is being sourced ..."