Skip to content

Instantly share code, notes, and snippets.

@bryder
Last active January 12, 2017 21:53
Show Gist options
  • Save bryder/8c8cdd81889edfd698e3 to your computer and use it in GitHub Desktop.
Save bryder/8c8cdd81889edfd698e3 to your computer and use it in GitHub Desktop.
get dirname/basename and full path of a script in bash
# 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