This is very useful in scripts where you have a menu to allow the script itself to be edited.

DIR=”$( cd -P “$( dirname “${BASH_SOURCE[0]}” )” && pwd )”

Source: Stackoverflow