Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@josephwecker
Created June 6, 2012 19:55
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 josephwecker/2884332 to your computer and use it in GitHub Desktop.
Save josephwecker/2884332 to your computer and use it in GitHub Desktop.
Robustly get script's actual directory (bash)
#!/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