KSH - Sourced file location

The sample scripts below, I am looking for the location of the sourced b.sh file. The b.sh file is source by multiple files, so it is not feasible to rely on a "global" variable or passed argument. Is there any way to determine the location of b.sh within b.sh?

a.sh

 
#!/bin/ksh
echo "a: <$0>"
ScriptDir=`dirname $0`
. ${ScriptDir}/b.sh

b.sh

 
#!/bin/ksh
WhereAmI="????"
echo "b: <$0> ${WhereAmI}"

b.sh

#!/bin/ksh

thisFILE="$(whence ${0})"
thisDIR="$(dirname ${thisFILE})"
PROGNAME="${0##*/}"

WhereAmI="${thisDIR}"
echo "b: <${PROGNAME}> ${WhereAmI}"

Note: this works if a.sh and b.sh reside in the same direct. If b.sh is somewhere else, it returns a.sh location (and rightfully so). Need to think about that.

Thanks for the response, but as you indicate at the end of your post, it only works when a and b are in the same directory. My useCase has to address the notion that a is in a different directory and not necessarily always a.

The useCase is that /app1/a.sh sources /common/b.sh
/app2/c.sh sources /common/b.sh

I need b.sh to know that he is in the common directory.

this lnk might be helpful....
Bit I could not make it work with bash/ksh on Solaris....
Maybe others will have better ideas.

Not really much help. The link tried to provide workarounds rather than answering the question. There are times when inside the script, it doesn't know what directory it is in. Seems like a $0 equivalent should be available for a sourced file.

You'd think , huh?
But it ($0 in the sourced shell) is not what what it's in the 'called' (not sourced) script - as the 'sourced' script runs in the same process space (there's no newly forked process) as the 'sourcing' script. Therefore, the '$0' in the sourced script is the '$0' of the sourcing script.

I'm not sure if I'm explaining this correctly - you can take a look at this thread or browse this comp.unix.shell user group for similar threads.

Understand it completely.

Again, I was looking (hoping) for a $0 equivalent for the sourced file. I realize the $0 is the invoked script.

Thanks for the reponses, hopefully someone will provide some insight.

Why? There is rarely a good reason for doing that.

Why do you need to rely on anything? Put the script in a directory that is in your $PATH.

There's no need for an external command:

ScriptDir=${0%/*}

The script is untouched as it migrates throught the life cycle regions with various file sub-systems, such as /common/dev, /common/test, and so on. Specific applications /app1 or /app2 will be invoked from an external scheduler using a parameter. So with in the /app1 script, they run $1/setCommonEnv.sh where $1 is /common/dev or /common/test ...

Within setCommonEnv.sh, i would like to derive which environment I am in, as well as which script or subscript.

Seems very reasonable to be able to walk a call stack.

If you are using ksh93 it can be easily done using the .sh.file builtin parameter

$ cat a.sh
#!/bin/ksh93

echo "a: $PWD"

. ./ksh93/b.sh
$
$cat ksh93/b.sh
#!/bin/ksh93

echo "b: $PWD"
echo "b: ${.sh.file}"
echo "b: ${.sh.file##*/}"
echo "b: ${.sh.file%/*}"
$
$ ./a.sh
a: /home/fpm
b: /home/fpm
b: /home/fpm/ksh93/b.sh
b: b.sh
b: /home/fpm/ksh93
$

Exactly what I am looking for. However, the scripts are not running ksh93. I did run through a test cycle here on AIX 5.3, the results are below.

 
/test> cat a.sh
#!/bin/ksh93
echo "a: $0"
echo "a: $PWD"
ScriptDir=`dirname $0`
. ./sub/b.sh
/test> cat sub/b.sh
#!/bin/ksh93
echo "b: $PWD"
echo "b: ${.sh.file}"
echo "b: ${.sh.file##*/}"
echo "b: ${.sh.file%/*}"
/test> ./a.sh
a: ./a.sh
a: /test
b: /test
b:
b:
b:
/test>

Thanks for the time and response.

looks like the '.sh.file' is not implemented on AIX's version of ksh93 (among others).