Bash: detect execute vs source in a script?
I have a script that is littered around my filesystem (side effect of a build process) that sets some global environment variables (LD_LIBRARY_PATH, PATH, ROOTDIR, etc) that need to be in my shell for proper operation. Of course, that means I need to source script.sh
or . script.sh
to get it to work, and not ./script.sh
(which would spawn a new shell and not set the variables in my current shell).
Is there anything I can add to the script to prevent (or warn) someone running ./script.sh
?
Solution 1:
In a shell script, $0
is the name of the currently running script. You can use this to tell if you're being sourced or run like this:
if [[ "$(basename -- "$0")" == "script.sh" ]]; then
echo "Don't run $0, source it" >&2
exit 1
fi
Solution 2:
Simplest way in bash
is:
if [ "$0" = "$BASH_SOURCE" ]; then
echo "Error: Script must be sourced"
exit 1
fi
$BASH_SOURCE
always contains the name/path of the script.
$0
only contains the name/path of the script when NOT sourced.
So when they match, that means the script was NOT sourced.
Solution 3:
This has been discussed on SO. The most-upvoted answer by @barroyo says to use
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && echo "script ${BASH_SOURCE[0]} is being sourced ..."
Solution 4:
Another option may be to remove execute permissions. In that case it can not be executed but it can still be sourced.