Skip to content

Instantly share code, notes, and snippets.

@0cjs
Created February 28, 2017 05:17
Show Gist options
  • Save 0cjs/2c0f78c21649af8466e9b50c1e5f7df8 to your computer and use it in GitHub Desktop.
Save 0cjs/2c0f78c21649af8466e9b50c1e5f7df8 to your computer and use it in GitHub Desktop.
Find true base directory of current shell script, regardless of symlinks
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