Last active
January 12, 2017 21:53
-
-
Save bryder/8c8cdd81889edfd698e3 to your computer and use it in GitHub Desktop.
get dirname/basename and full path of a script in 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
# This will work even with spaces in the directory name or the script name | |
# or even if the script is sourced | |
script_basename=${BASH_SOURCE[0]##*/} | |
# on osx you need to brew install coreutils - then you get greadlink not readlink as in linux | |
script_fullpath=$( (greadlink -f "${BASH_SOURCE[0]}" || readlink -f "${BASH_SOURCE[0]}" ) 2> /dev/null) | |
script_dirname=${script_fullpath%/*} | |
echo basename \'${script_basename}\' | |
echo fullpath \'${script_fullpath}\' | |
echo dirname \'${script_dirname}\' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment