On Fri, 2008-02-29 at 01:33 -0600, Mike Miller wrote:
> #!/bin/bash --
> EXEC_DIR=$(builtin cd -P -- "${0%/*}" && builtin pwd)
> echo "$EXEC_DIR"

[...]

> Regarding the above script, is the "--" needed after "bash"?  I don't 
> think it is because there is nothing else on that line.

The '--' is there for the outside case that you've executed the script
from a directory that starts with a dash.

If I create a directory '-P' and put my script in it, I could see this:

        [mike at 3po][~]$ -P/execdir 
        /bin/bash: -/: invalid option
        Usage:	/bin/bash [GNU long option] [option] ...
        	/bin/bash [GNU long option] [option] script-file ...
        GNU long options:
        	--debug
        	--debugger
        [...]

My command had been expanded to be 'bash -P/execdir'.  If you add the
two dashes at the top of the script, the actual command executed will
instead be 'bash -- -P/execdir'.

Of course, having a directory with a dash as the first character is
really unlikely, and having actual executables in that directory is a
few orders of magnitude even less likely.  It was basically an exercise
in making the script as bombproof as possible.

-- 
Mike Hicks <hick0088 at tc.umn.edu>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://mailman.mn-linux.org/pipermail/tclug-list/attachments/20080229/f2caf0d9/attachment.pgp