Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 or . to get it to work, and not ./ (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 ./ ?

share|improve this question
up vote 5 down vote accepted

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")" == "" ]]; then
    echo "Don't run $0, source it" >&2
    exit 1
share|improve this answer

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 ..."
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.