06 Feb, 2009
How to get the absolute path within the running bash script
Posted by: Fritz Thomas In: Linux
Have you ever needed the absolute path of the shell script within this script? I do not mean the current working dir (pwd). That is different, because you do not know from which path the bash script is called.
In my case i needed the absolute path, because i wanted to call another script, which is by convention, in the same directory as the called script. In such a case you would use realpath in PHP – readlink is the bash equivalent to it.
1 2 3 4 | # Absolute path to this script. /home/user/bin/foo.sh SCRIPT=$(readlink -f $0) # Absolute path this script is in. /home/user/bin SCRIPTPATH=`dirname $SCRIPT` |