Building notes, projects, and occasional rants

Lazyweb request: how to find full path to script

Dear Lazyweb,

this is something that I searched for quite some time and could not find yet, maybe a helpful soul knows.

In a bourne shell script, how can I find the full path of the script being executed?

Thanks in advance.

Update: the following command, sent by Celso Pinto, passes all my tests.

SCRIPTDIR="cd $(dirname $0);pwd

For the record, I used two scripts to test this. First, the script:


echo "\$0 is $0" SCRIPTDIR=dirname $0 echo " dir: $SCRIPTDIR" SCRIPTDIR="cd $(dirname $0);pwd" echo " dir: $SCRIPTDIR"

The script to run the tests,


chmod 755

./ 1 ../$USER/ 2 ../$USER/bin/../ 3

sh ./ 1 with sh sh ../$USER/ 2 with sh sh ../$USEr/bin/../ 3 with sh

export PATH=pwd:$PATH

cd / "via path"