marcus-g
5/5/2017 - 7:43 PM

Get currently running script's containing directory, stored as $dir

Get currently running script's containing directory, stored as $dir

# foolproof way to figure out where this script is placed
source="${BASH_SOURCE[0]}"
while [ -h "$source" ]; do # resolve $source until the file is no longer a symlink
  dir="$( cd -P "$( dirname "$source" )" && pwd )"
  source="$(readlink "$source")"
  # if $source was a relative symlink, we need to resolve it relative
  # to the path where the symlink file was located
  [[ $source != /* ]] && source="$dir/$source"
done
dir="$( cd -P "$( dirname "$source" )" && pwd )" #dir now has the script locat'n