Wednesday, February 10, 2010

Bash script to find the location of the currently running script

#!/bin/bash
#==========================
# bash - find path to script
#==========================
abspath="$(cd "${0%/*}" 2>/dev/null; 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

(not my script, I have copied from here http://forums.macosxhints.com/archive/index.php/t-73839.html )


Otherwise, this is much simpler:

SCRIPT=`readlink -f $0`
SCRIPTPATH=`dirname $SCRIPT`
echo $SCRIPTPATH

No comments: