On Wed, 27 Feb 2008, Steve Cayford wrote:

> Mike Miller wrote:
>> On Wed, 27 Feb 2008, Kathryn Hogg wrote:
>>
>>> If you want to print just the directory that the script is located in 
>>> do
>>>
>>> (cd $(dirname $0); pwd)
>>
>> That assumes that the script is in "dirname $0" but what if the script 
>> is in /usr/local/bin and /usr/local/bin is in the path and the user 
>> typed this:
>>
>> script.bash
>>
>> Then "dirname $0" will fail, and that is why the original poster 
>> suggested use of "which".  Also, you need quotes around the $0.
>
> Hm. Not on my system. When I run a script in /usr/local/bin by typing 
> test.sh, `dirname "$0"` returns /usr/local/bin. The path is getting 
> expanded before the script runs.
>
> That seems to work when running sh, bash, or tcsh at the command line or 
> in the script itself.


My apologies to Kathryn Hogg.  It seems that you are correct because if I 
run "echo $0" within the script it returns the full path from $PATH even 
when nothing but the filename was typed on the command line.

Another guy named Brock Noland wrote to tell me that "which" in Solaris 
does not return the full path.  He is quite right about that.  I guess it 
is a GNU thing.  He also thought Kathryn's method would work.

So then, does Kathryn's method always work? (with quotes around $0):

(cd $(dirname "$0"); pwd)

I think maybe it does.  It is an elegant solution.

Mike