How to obtain the full absolute path of the exectuing shell script without readlink

One issue that I always come across when creating a shell script is referencing files dynamically based on the location of the shell script. A hack I often used was to pass the script’s path to the script on execution because the readlink command was not available to me. However recently I have revisited this issue and have found a solution for obtaining the absolute path of the executing shell script without readlink. For your convenience both scripts are found below.

Absolute Path using readlink

PATH= $(dirname $(readlink -f $0))
echo ${PATH}

Absolute Path without using readlink

cd "`dirname ${0}`"


echo ${PATH}
// Tips & Tricks // Unix //

Comments & Questions

Add Your Comment