In bash scripts we often need to know in which directory the running script is found especially when the script is in the $PATH and may be occurring in multiple places:
Here is a reliable way to find it out:
Based on this site:
http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=$(dirname $SCRIPT)