Posts Tagged ‘bash’

Determining own directory path in unix bash script

August 12, 2011

In a unix bash script, you can determine the scripts filesystem location by using

SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"

This is especially useful if you want to use “relative” (well, sort of) paths within your script, regardless of which directory the script is being executed from, e.g.

java -jar $SCRIPT_DIR/libs/some-file.jar

If you just used libs/some-file.jar the bash script would only work when executed from the directory it is located in.