32

Either what I'm asking here is extremely unorthodox/unconventional/risky, or my Google-fu skills just aren't up to snuff...

In a bash shell script, is there any easy of way of telling if it is getting sourced by another shell script, or is it being run by itself? In other words, is it possible to differentiate between the following two behaviors?

# from another shell script
source myScript.sh

# from command prompt, or another shell script
./myScript.sh

What I'm thinking of doing is to create an utilities-like shell script containing bash functions that can be made available when sourced. When this script is being run by itself though, I'll like it to perform certain operations, based on the defined functions too. Is there some kind of an environment variable that this shell script can pick up on, e.g.

some_function() {
    # ...
}
if [ -z "$IS_SOURCED" ]; then
    some_function;
fi

Preferably, I'm looking for a solution that doesn't require the caller script to set any flag variables.

edit: I know the difference between sourcing and and running the script, what I'm trying to find out here if it's possible to tell the difference in the script that is being used (in both ways).

4
  • 1
    possible duplicate of running script with ". " and with "source "
    – cuonglm
    Commented Jul 6, 2015 at 2:25
  • @cuonglm edited my question, I know the differences between the both but I'm wondering if I can programmatically make the shell script tell the difference as well.
    – h.j.k.
    Commented Jul 6, 2015 at 2:31
  • 5
    @cuonglm: Not a duplicate. He's not asking about the . command at all, but about detecting whether a script has been sourced or run normally (i.e. in a subshell).
    – Jander
    Commented Jul 6, 2015 at 6:21
  • Very good answers on same question at stack overflow: stackoverflow.com/a/28776166/96944 Commented Feb 6, 2018 at 6:49

5 Answers 5

23

Yes - the $0 variable gives the name of the script as it was run:

$ cat example.sh
#!/bin/bash
script_name=$( basename ${0#-} ) #- needed if sourced no path
this_script=$( basename ${BASH_SOURCE} )
if [[ ${script_name} = ${this_script} ]] ; then
    echo "running me directly"
else
    echo "sourced from ${script_name}"
fi 

$ cat example2.sh
#!/bin/bash
. ./example.sh

Which runs like:

$ ./example.sh
running me directly
$ ./example2.sh
example.sh sourced from example2.sh

That doesn't cater for being source from an interactive shell, but you get this idea (I hope).

Updated to include BASH_SOURCE - thanks h.j.k

1
  • Close enough. :) Small hindrance that I'll need to specify the script's name at least, but I'll take this answer if there are no other viable solutions...
    – h.j.k.
    Commented Jul 6, 2015 at 2:40
11

Combining @DarkHeart's answer with the environment variable BASH_SOURCE seems to do the trick:

$ head example*.sh
==> example2.sh <==
#!/bin/bash
. ./example.sh

==> example.sh <==
#!/bin/bash
if [ "$(basename $0)" = "$(basename $BASH_SOURCE)" ]; then
    echo "running directly"
else
    echo "sourced from $0"
fi
$ ./example2.sh
sourced from ./example2.sh
$ ./example.sh
running directly

edit Seems to be a simpler solution still if I were to just count the number of elements in BASH_SOURCE's array:

if [ ${#BASH_SOURCE[@]} -eq 1 ]; then echo "running directly"; else echo "sourced from $0"; fi
2
  • 2
    Looks like we found the 'bash_source' variable at the same time. :)
    – user14755
    Commented Jul 6, 2015 at 2:58
  • @DarkHeart I've added onto my answer the usage of counting the array size too... thanks for hinting me on this path! :D
    – h.j.k.
    Commented Jul 6, 2015 at 3:07
8

I just created the same kind of library script that works alot like BusyBox. In it, I use the following function to test if it is being sourced...

function isSourced () {
  [[ "${FUNCNAME[1]}" == "source" ]]  && return 0
  return 1
}

The Bash-maintained FUNCNAME array is essentially a function call stack. $FUNCNAME (or ${FUNCNAME[0]}) is the name of the currently executing function. ${FUNCNAME[1]} is the name of the function that called it, and so on.

The topmost item is a special value for the script itself. It will contain...

  • the word "source" if the script is being sourced
  • the word "main" if the script is being executed AND the test is being done from within a function
  • ""(null) if the script is being executed AND the test is being done outside of any function, that is... at the level of the script itself.

The function above actually only works when called at the script level (which is all I needed). It would fail if called from inside another function because the array item number would be wrong. To make it work anywhere requires finding the top of the stack and testing that value, which is more complicated.

If you need that, you can get the array item number of the "top of the stack" with...

  local _top_of_stack=$(( ${#FUNCNAME[@]} - 1 ))

${#FUNCNAME[@]} is the number of items in the array. As a zero-based array, we subtract 1 to get the last item#.

These three functions are used together to produce a function stack trace similar to Python's and they may give you a better idea how all this works...

function inspFnStack () {
  local T+="  "
  local _at=
  local _text="\n"
  local _top=$(inspFnStackTop)
  local _fn=${FUNCNAME[1]}; [[ $_fn =~ source|main ]]  || _fn+="()"
  local i=_top; ((--i))
  #
  _text+="$i item function call stack for $_fn ...\n"
  _text+="| L   BASH_SOURCE{BASH_LINENO called from}.FUNCNAME  \n"
  _text+="| ---------------------------------------------------\n"
  while (( $i > 0 ))
  do
    _text+="| $i ${T}$(inspFnStackItem $i)\n"
    T+="  "
    ((--i))
  done
  #
  printf "$_text\n"
  #
  return 0
}

function inspFnStackItem ()  {
  local _i=$1
  local _fn=${FUNCNAME[$_i]}; [[ $_fn =~ source|main ]]  || _fn+="()"
  local _at="${BASH_LINENO[$_i-1]}"; [[ $_at == 1 ]]  && _at="trap"
  local _item="${BASH_SOURCE[$_i]}{${_at}}.$_fn"
  #
  printf "%s" "$_item"
  return 0
}

function inspFnStackTop () {
  # top stack item is 1 less than length of FUNCNAME array stack
  printf "%d\n" $(( ${#FUNCNAME[@]} - 1 ))
  #
  return 0
}

Note that FUNCNAME, BASH_SOURCE and BASH_LINENO are 3 arrays maintained by bash as if they were one three-dimensional array.

1
1

One way which also works when sourcing from an interactive shell:

if [ $BASH_LINENO -ne 0 ]; then
    some_function;
fi

The BASH_LINENO variable is also an array with all the lines the calling function was executed at. It will be zero if you call the script directly, or an integer corresponding to a line number.

The BASH_* variable docs

0

Just want to add that counting the array appears to be unreliable and one should probably not assume source was used since using a dot (.) is very common as well (and predates the source keyword).

For example, for a sourced.sh script containing only echo $0:


$ . sourced.sh 
bash
$ source sourced.sh 
bash
$ chmod +x sourced.sh 
$ ./sourced.sh 
./sourced.sh
$ cat ./sourced.sh 
echo $0

The comparison solutions suggested work better.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .