Ma99uS Ma99uS - 2 months ago 11
Linux Question

How to get script file path inside script itself when called through sym link

When I need to get path to the script file inside script itself I use something like this:

`dirname $0`


that works file until I call the script through sym link to it. In that case above code prints the location of the link instead the original file.

Is there a way to get the path of the original script file, not the link?

Thanks,
Mike

dsm dsm
Answer
if [ -L $0 ] ; then
    DIR=$(dirname $(readlink -f $0)) ;
else
    DIR=$(dirname $0) ;
fi ;