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

#!/bin/sh
PATH= $(dirname $(readlink -f $0))
echo ${PATH}

Absolute Path without using readlink

#!/bin/sh
ORIGINAL_PATH=`pwd`
cd "`dirname ${0}`"

PATH=`pwd`
cd "${ORIGINAL_PATH}"

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

Comments & Questions

Add Your Comment