On Wed, 27 Feb 2008, Mike Hicks wrote:

> On Wed, 2008-02-27 at 19:26 -0600, Kathryn Hogg wrote:
>> If you want to print just the directory that the script is located in do
>>
>> (cd $(dirname $0); pwd)
>
> This looks like a winner to me.  To handle spaces and at least some
> other nastiness, this seems to work
>
> (cd "$(dirname "$0")"; pwd)
>
> I've had it work with directories including newlines and backslashes. I
> was a little surprised, but apparently bash separately parses
> double-quotes which are within $( .. ) blobs.

I guess you can do plenty of nesting of $() terms also.  I was pleasantly 
surprised to see that it handled newlines because the one I wrote earlier 
did not handle them correctly.


> I might change the semicolon to '&&'.  If there's somehow an error, 
> you'd most likely get a zero-length return string instead of the 
> original workind dir, and $? would be set non-zero.

That's another new one for me -- I didn't know that "&&" could be used 
instead of the semicolon.

Steve Crayford was definitely right about this:

    Note that if $0 is a relative path and you change directories in your
    script you'll no longer get the right result. However you should be
    able to do this early in the script and save the path for later if
    necessary.

Good call, Steve.

Mike