Monday, 19 May 2014

How to Get the Absolute Path of the Currently Executing Script

Assign the directory of the currently executing script to a variable and then print it to the standard out:

pushd `dirname $0` > /dev/null
PATH_TO_SCRIPT=`pwd -P`popd > /dev/null
echo "Path:" $PATH_TO_SCRIPT;

Explanation:

1. Move to the directory of the script
2. Assign PATH_TO_SCRIPT to the absolute path of the current directory resolving symbolic links
3. Move back to the original directory
4. Print out the path 

No comments:

Post a Comment