How to get the name of a sourced script in bash
You can easily get the name of a the script which is executing using $0, however, if the script is being sourced, then $0 will return the parent script. I was looking for something like Python's __FILE__ variable, and I found BASH_SOURCE. Here's an example of how to use it.
<geshi lang="bash">
- !/bin/bash
echo "My parent is $0 and is executing in $(cd $(dirname $0); pwd)"
echo "I am $BASH_SOURCE and I'm executing in $(cd $(dirname $BASH_SOURCE); pwd)" </geshi>
<geshi lang="bash"> $ . whereami.sh My parent is bash and is executing in /cygdrive/c/dev I am /tmp/whereami.sh and I'm executing in /tmp </geshi>