Thursday, May 21, 2009

Get the directory of the currently running script


# $0 gives the relative path the script was invoked with, so:
#    ./script.sh gives ./script.sh
#    ~/script.sh gives /home/user/script.sh
#
# $(pwd) prints the working directory, not the script file location
#
# `which $0` is a special use to get the full path and name of the script
# Called this way, which does not do its normal search of $PATH
#
# Enclose in double quotes to account for folders with spaces in them

  RUNDIR=$(dirname "$(which "$0")")

No comments:

Post a Comment