Reliable way for a Bash script to get the full path to itself - Stack Overflow

SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"