Quick Tip: Get Absolute Path to Directory Script is Running in

- Tuesday, January 17, 2023

When running a bash script, it’s sometimes convenient to get a fully qualified path to the directory that the source of the running script is in.

I continually have to remind myself of the exact syntax for this tip, so I’ve posted it for posterity. This approach should work around most issues of symlinked files, sourced files, etc.

Here’s the snippet:

1
2
3
#!/usr/bin/env bash

SCRIPTDIR="$( cd -P "$(dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)"
Bash Tip
Software