Get path of current script when executed through a symlink
Try this as a general purpose solution:
DIR="$(cd "$(dirname "$0")" && pwd)"
In the specific case of following symlinks, you could also do this:
DIR="$(dirname "$(readlink -f "$0")")"
one line:
cd $(dirname $([ -L $0 ] && readlink -f $0 || echo $0))
readlink -f
doesn't work for me, I've this mistake:
readlink: illegal option -- f
usage: readlink [-n] [file ...]
But this works fine:
DIR="$(dirname "$(readlink "$0")")"
echo $DIR