Skip to content

Instantly share code, notes, and snippets.

@jorgevila
Created May 28, 2012 10:30
Show Gist options
  • Save jorgevila/2818406 to your computer and use it in GitHub Desktop.
Save jorgevila/2818406 to your computer and use it in GitHub Desktop.
Bash get script path
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