Hi,
I'm not sure exactly what won't work,
its probably just my misunderstanding of what the OP wants.
However, all my tests show it does work even the two cases
you showed me above.
It was my understanding his desire was to know the location
of the script, and the code I posted seems to do that,
am I missing something... just wanted to know.
Code:
[0 randyd@entropy ~]$ cat t.sh
#!/bin/sh
wd="`pwd`"
cd "`dirname "$0"`"
programdir="`pwd`"
cd "$wd"
echo "$programdir"
[0 randyd@entropy ~]$ ./t.sh
/home/randyd
[0 randyd@entropy ~]$ bash t.sh
/home/randyd
[0 randyd@entropy ~]$ cd ..
[0 randyd@entropy home]$ randyd/t.sh
/home/randyd
[0 randyd@entropy home]$