[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is there a special variable for the directory where the script is in
From: |
Eric Blake |
Subject: |
Re: Is there a special variable for the directory where the script is in? |
Date: |
Thu, 11 Feb 2010 18:26:19 -0700 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 |
According to Chris F.A. Johnson on 2/11/2010 4:23 PM:
> On Fri, 12 Feb 2010, Peng Yu wrote:
>
>> $0 gives the file name of the script. I could use several shell
>> command to get the directory where the script is in. But I'm wondering
>> if there is an easy-to-use variable that refers to the directory where
>> the script is in?
>
> $0 normally gives the full path to the file, so: "${0%/*}"
No, $0 normally gives the argv[0], which matches how the file was invoked.
If it was invoked by absolute path (or even an anchored invocation, such
as ./script), then you can compute it yourself. Otherwise, you have to
recreate the PATH walk in your script to determine where the script was
eventually located, and hope that the script was not invoked in such a way
that argv[0] does not match the name of the script being run.
Look at any configure script generated by autoconf for a sample of how
complex it can be to portably locate $myself.
--
Don't work too hard, make some time for fun as well!
Eric Blake ebb9@byu.net
signature.asc
Description: OpenPGP digital signature