[ale] How to expand to full-path name?

Ken Alexander kenry at alexandertech.biz
Thu Aug 9 11:54:27 EDT 2007


On Thu, 2007-08-09 at 10:57 -0400, Tim Watts wrote:

> Hi,
> 
> Suppose I have MyScript in /home/me/bin which does this:
> 
> 	#!/bin/sh
> 	SCRIPT_HOME=`dirname $0`
> 	echo home=$SCRIPT_HOME
> 
> Now I run it from /home/me/bin which produces:
> 
> 	home=.
> 
> How can I expand SCRIPT_HOME into the full path name (i.e. /home/me/bin)?
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale


$0 is whatever you typed in at the command line. So you can call the
script with the absolute path. Instead of "./script" use
"/full/path/to/script".

You can use `pwd` as someone already said, but that's going to give you
whichever directory you are in when the script is run. Not necessarily
the directory the script resides in.  So, if you need this to work from
anywhere, you can maybe do an if statement to check whether $0 gives you
an absolute path. 

quick and dirty ....

script_home=$(dirname $0)
if [ ${script_home:0:1} != '/' ]
then
   cd ${script_home}
   home=`pwd`
   cd - >  /dev/null
fi
echo home=${script_home}

-KA
-------------- next part --------------
An HTML attachment was scrubbed...




More information about the Ale mailing list