Skip to content

Instantly share code, notes, and snippets.

@agate
Last active December 16, 2015 22:20
Show Gist options
  • Save agate/5506697 to your computer and use it in GitHub Desktop.
Save agate/5506697 to your computer and use it in GitHub Desktop.
DIR is the real directory of this script
SOURCE="${BASH_SOURCE[0]}"
# resolve $SOURCE until the file is no longer a symlink
while [ -h "$SOURCE" ]; do
TARGET="$(readlink "$SOURCE")"
if [[ $SOURCE == /* ]]; then
SOURCE="$TARGET"
else
DIR="$( dirname "$SOURCE" )"
# if $SOURCE was a relative symlink, we need to resolve it relative
# to the path where the symlink file was located
SOURCE="$DIR/$TARGET"
fi
done
RDIR="$( dirname "$SOURCE" )"
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment