Created
January 23, 2018 14:28
-
-
Save pauca/a12549df677e63493c28be384089f8b6 to your computer and use it in GitHub Desktop.
get_script_dir with bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set +u | |
get_script_dir () { | |
SOURCE="${BASH_SOURCE[0]}" | |
# While $SOURCE is a symlink, resolve it | |
while [ -h "$SOURCE" ]; do | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
SOURCE="$( readlink "$SOURCE" )" | |
# If $SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory | |
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" | |
done | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
echo "$DIR" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment