Created
June 6, 2012 19:55
-
-
Save josephwecker/2884332 to your computer and use it in GitHub Desktop.
Robustly get script's actual directory (bash)
This file contains hidden or 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
#!/bin/bash | |
# Put's the script's directory in the $BS variable. | |
# Follows symlinks, works on mac-os as well as linux etc. | |
# Move final popd to the end of your script if you want to run your script with the script's directory as the working directory. | |
BS="${BASH_SOURCE[0]}";RL="readlink";([[ `uname -s`=='Darwin' ]] || RL="$RL -f") | |
while([ -h "${BS}" ]) do BS=`$RL "${BS}"`; done | |
N="/dev/null";pushd .>$N;cd `dirname ${BS}`>$N;BS=`pwd`;popd>$N | |
# ... rest of the script ... use ${BS} when referring to this script's directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment