How to get the name of a sourced script in bash

From EggeWiki
Revision as of 02:03, 10 July 2009 by Brianegge (talk | contribs) (Created page with '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 someth…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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">

  1. !/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>