Last active
November 23, 2017 07:05
-
-
Save 27Bslash6/808da5a41884cce832fb93a5650c23e3 to your computer and use it in GitHub Desktop.
Reliably determine the path to the current script
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
#!/usr/bin/env bash | |
set -eu | |
# ---------------------------------------------------------------------------- | |
# Find real file path of current script | |
# https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within | |
# ---------------------------------------------------------------------------- | |
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")" | |
[[ $source != /* ]] && source="$dir/$source" # if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located | |
done | |
ROOT_DIR="$( cd -P "$( dirname "$source" )" && pwd )" | |
export ROOT_DIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment