Skip to content

Instantly share code, notes, and snippets.

@27Bslash6
Last active November 23, 2017 07:05
Show Gist options
  • Save 27Bslash6/808da5a41884cce832fb93a5650c23e3 to your computer and use it in GitHub Desktop.
Save 27Bslash6/808da5a41884cce832fb93a5650c23e3 to your computer and use it in GitHub Desktop.
Reliably determine the path to the current script
#!/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