Skip to content

Instantly share code, notes, and snippets.

@mikey179
Created March 28, 2017 13:11
Show Gist options
  • Save mikey179/595f9ed8c95297d15ca67fd5fbca02fd to your computer and use it in GitHub Desktop.
Save mikey179/595f9ed8c95297d15ca67fd5fbca02fd to your computer and use it in GitHub Desktop.
How to retrieve the full path where a script resides
#!/bin/bash
set -o nounset
set -o errexit
selfbase() {
local selfbase=""
if [ -f $0 -a ! -h $0 ]; then
selfbase="$(dirname $0)"
elif [ $(uname) = "Darwin" ]; then
selfbase="$(dirname $(readlink $0))"
else
selfbase="$(dirname $(readlink -f $0))"
fi
if [ $selfbase = "." ]; then
selfbase=$(pwd)
fi
echo $selfbase
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment