Skip to main content

Resolve the path of the running Bash script until the file is no longer a symlink.

#!/usr/bin/env bash

set -ue

source="${BASH_SOURCE[0]}"

#
# Resolve $source path of this script until the file is no longer a symlink
# https://github.com/dotnet/runtime/blob/master/eng/build.sh
while [[ -L $source ]]; do
    scriptroot="$(cd -P "$(dirname "$source")" && pwd)"
    source="$(readlink "$source")"
    # if $source was a relative symlink, we need to resolve it relative to the path where the
    # symlink file was located
    [[ $source != /* ]] && source="$scriptroot/$source"
done
scriptroot="$(cd -P "$(dirname "$source")" && pwd)"

echo "source .......: ${source}"
echo "scriptroot ...: ${scriptroot}"