Skip to content

Instantly share code, notes, and snippets.

@veryeasily
Last active November 18, 2021 09:10
Show Gist options
  • Save veryeasily/679e457931c134fe87cb282036637b3e to your computer and use it in GitHub Desktop.
Save veryeasily/679e457931c134fe87cb282036637b3e to your computer and use it in GitHub Desktop.
[Get dir of current script] Gets the directory of the currently running script without getting tricked by symlinks #Shell #Zsh #Bash
#!/usr/local/bin/zsh
# @description Gets the physical location of the current script in a way that
# doesn't get tricked by symlinks
# @see https://stackoverflow.com/questions/59895/how-can-i-get-the-source-directory-of-a-bash-script-from-within-the-script-itsel
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
# next works for sourced files
# @see https://unix.stackexchange.com/a/4673/37091
SCRIPT_DIR="$(cd -- "$(dirname -- "$_")" &> /dev/null && pwd)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment