Skip to content

Instantly share code, notes, and snippets.

@honzahommer
Last active March 4, 2019 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzahommer/68c547f074c421434ddd985a87ba069c to your computer and use it in GitHub Desktop.
Save honzahommer/68c547f074c421434ddd985a87ba069c to your computer and use it in GitHub Desktop.
Get the source directory of a Bash script from within the script itself
#!/usr/bin/env bash
__dirname(){ local d s="$0";while [ -h "$s" ];do d="$(cd -P "$(dirname "$s")">/dev/null 2>&1&&pwd)";s="$(readlink "$s" 2>&1)";[[ $s != /* ]]&&s="$d/$s";done;echo "$(cd -P "$(dirname "$s")">/dev/null 2>&1&&pwd)";}
#!/usr/bin/env bash
__dirname() {
local d s="$0"
while [ -h "$s" ]; do
d="$(cd -P "$(dirname "$s")" >/dev/null 2>&1 && pwd)"
s="$(readlink "$s" 2>&1)"
[[ $s != /* ]] && s="$d/$s"
done
echo "$(cd -P "$(dirname "$s")" >/dev/null 2>&1 && pwd)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment