Under most circumstances, the following gets the absolute path to the directory that the script is in:
PARENT_FOLDER=$(dirname "$(readlink -f "$0")")
(OSX and BSD might lack the -f
option without which the path may still contain symbolic links.)
If you just want the name of the final directory in the path, just add one step:
PARENT_FOLDER=$(dirname "$(readlink -f "$0")")
PARENT_FOLDER=${PARENT_FOLDER##*/}
On systems lacking readlink -f
but for which bash is available:
PARENT_FOLDER="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PARENT_FOLDER=${PARENT_FOLDER##*/}
And if lacking both readlink -f
and bash:
PARENT_FOLDER="$( cd "$( dirname "$0" )" && pwd )"
PARENT_FOLDER=${PARENT_FOLDER##*/}