Skip to content

Instantly share code, notes, and snippets.

@jpickwell
Last active September 3, 2025 17:30
Show Gist options
  • Save jpickwell/3246190f4538bf67d8319f3c90615976 to your computer and use it in GitHub Desktop.
Save jpickwell/3246190f4538bf67d8319f3c90615976 to your computer and use it in GitHub Desktop.
Bash __dirname
# shellcheck disable=SC2311
__dirname="$(_dn(){ local s="$1" d; while [[ -h "${s}" ]]; do d="$(cd -P -- "$(dirname -- "${s}")" 1>/dev/null 2>&1 && pwd -P)"; s="$(readlink "${s}")"; [[ "${s}" == /* ]] || s="${d}/${s}"; done; cd -P -- "$(dirname -- "${s}")" 1>/dev/null 2>&1 && pwd -P; }; _dn "${BASH_SOURCE[0]}")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment