Skip to content
Advertisement

Get the name of the directory where a script is executed

I have some script, that uses files in directories around it. It uses

dirname $0

command. It should work from any directory where I run this script, but when I run a symbolic link that points to that script I get the path of symbolic link. So I get the output of dirname rather than the path of the script itself.

Any one know a way to get the path of where the script is run?

Advertisement

Answer

Get the real path to your script

if [ -L $0 ] ; then
    ME=$(readlink $0)
else
    ME=$0
fi
DIR=$(dirname $ME)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement