Last active
March 4, 2019 23:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)";} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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