Get the directory of the executing bash script

04 Apr 2019
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

DIR will be set to the path of the directory which the given bash script is stored in.

Note that this call is specific to bash and is not POSIX.

This line works for sourcing and executing the script however if the script is read from a pipe, then the DIR will be set to the current directory