Should use $BASH_SOURCE in place of $0 so it works in files that are sourced.
The incantation at the top of all my scripts, that I learned from other HN folks, is:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
And I guess throw a realpath in there too for symlinks, per TFA.