« No more bazzilion git-* commands | Main | Middle-range Macs »

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_dir.sh script:

#!/bin/sh

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

The script to run the tests, run_script_dir_tests.sh:

#!/bin/sh

chmod 755 script_dir.sh

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

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

export PATH=`pwd`:$PATH

cd /
script_dir.sh "via path"