Skip to content

Instantly share code, notes, and snippets.

@jschnare
Created May 25, 2010 18:44
Show Gist options
  • Save jschnare/413510 to your computer and use it in GitHub Desktop.
Save jschnare/413510 to your computer and use it in GitHub Desktop.
# from: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-in
# credits: 3bcdnlklvc04a, e-satis
# Works for all versions,including when called via multple depth soft link,
# when script called by command "source" aka . (dot) operator. when arg $0 is
# modified from caller. "./script" "/full/path/to/script"
# "/some/path/../../another/path/script" "./some/folder/script" SCRIPT_PATH is
# given in full path, no matter how it is called. Just make sure you locate
# this at start of the script.
# This comment and code Copyleft, selectable license under the GPL2.0 or later
# or CC-SA 3.0 (CreativeCommons Share Alike) or # later. (c) 2008. All rights
# reserved. No warranty of any kind. You have been warned.
# http://www.gnu.org/licenses/gpl-2.0.txt
# http://creativecommons.org/licenses/by-sa/3.0/
SCRIPT_PATH="${BASH_SOURCE[0]}";
if([ -h "${SCRIPT_PATH}" ]) then
while([ -h "${SCRIPT_PATH}" ]) do SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
pushd . > /dev/null
cd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment