Created
May 28, 2012 10:30
-
-
Save jorgevila/2818407 to your computer and use it in GitHub Desktop.
Bash get script path
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
You can't use $0 because that gives only a relative path if the script is run with a command like ./myscript.sh. | |
The method below works well for both Linux and Mac OS X. It's a bit weird, but works. | |
#!/bin/bash | |
#========================== | |
# bash - find path to script | |
#========================== | |
abspath=$(cd ${0%/*} && echo $PWD/${0##*/}) | |
# to get the path only - not the script name - add | |
path_only=`dirname "$abspath"` | |
#display the paths to prove it works | |
echo $abspath | |
echo $path_only |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment