Skip to content

Instantly share code, notes, and snippets.

@pauca
Created January 23, 2018 14:28
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 pauca/a12549df677e63493c28be384089f8b6 to your computer and use it in GitHub Desktop.
Save pauca/a12549df677e63493c28be384089f8b6 to your computer and use it in GitHub Desktop.
get_script_dir with bash
#!/bin/bash
set -e
set +u
get_script_dir () {
SOURCE="${BASH_SOURCE[0]}"
# While $SOURCE is a symlink, resolve it
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$( readlink "$SOURCE" )"
# If $SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo "$DIR"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment