Finding a script's location (was: Re: comp.unix.questions)

Jim Rogers jimr at hp-lsd.COS.HP.COM
Thu Sep 13 03:39:54 AEST 1990


The following script seems to meet all your requirements:
i.e. works when executable is:

	in an absolute path

	in a relative path

	in a path specified by the PATH avariable.



#! /bin/ksh

# This script displays the path name of the script itself
# from the command argument $0

name=$0
bname=`basename $name`
ipth=${name%$bname}
if [ "$bname" = "$name" ]
then
# Script somewhere on the path
	fpth=`whence $name`
	pth=${fpth%/$bname}
else
	cd $ipth
	pth=$PWD
fi
print "Path to $0 is $pth"


Jim Rogers
Hewlett Packard Company



More information about the Comp.unix.shell mailing list