Home Blog Bash Script dir and path

Script dir and path

Get the directory of a script when it’s a regular file:

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)

Get the directory of a script when it’s a symlink:

SCRIPT_DIR=$(dirname "$(readlink -f "$0")")

Get the path of a script itself:

SCRIPT_PATH="${SCRIPT_DIR}/$(basename "$0")"

Sources