Bash scripting: check whether the script is sourced or executed in subshell








if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
    echo "sourced"
else
    echo "executed"
fi



The example below, call return if the script is sourced otherwise call exit:
my_func1 || ( if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then return 1; else exit 1 )


see also

  • https://groups.google.com/forum/#!topic/comp.unix.shell/gSa__gT81z0



No comments:

Post a Comment