dotkb

    Script dir and path

    (updated: )

    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

    https://stackoverflow.com/a/246128