Created
February 28, 2017 05:17
-
-
Save 0cjs/2c0f78c21649af8466e9b50c1e5f7df8 to your computer and use it in GitHub Desktop.
Find true base directory of current shell script, regardless of symlinks
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
set_basedir() { # This is portable; don't touch it! | |
local dir path0="$0" | |
while [ -h "$path0" ]; do | |
dir="$(cd "$(dirname "$path0")" && pwd -P)" | |
path0="$(readlink "$path0")"; | |
[ "$(echo "$path0" | cut -b 1)" = / ] || path0="$dir/$path0" | |
done | |
basedir="$(cd "$(dirname "$path0")/.." && pwd -P)" | |
}; set_basedir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment