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

Jay Loden ale at jayloden.com
Thu Aug 9 12:10:46 EDT 2007



Ken Alexander wrote:
>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

Since bash does not have an abspath or equivalent function, the only other way I
can think of to do this is:

script_home=`python -c "import os; print os.path.abspath(\"$(dirname $0)\")"`

or if you prefer Perl:

script_home=`perl -e "use Cwd 'abs_path'; print abs_path(\"$(dirname $myscript)\")"`

-Jay



More information about the Ale mailing list