Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created July 19, 2022 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrnold/4e375809a4fe16fcac383ab03822b1fb to your computer and use it in GitHub Desktop.
Save jrnold/4e375809a4fe16fcac383ab03822b1fb to your computer and use it in GitHub Desktop.
Resolve bash script location
# original source of this is quarto
# Determine the path to this script (we'll use this to figure out relative positions of other files)
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && 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
export SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment