On Fri, 29 Feb 2008, Mike Hicks wrote:

> 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'.

That is very interesting.  I did not know that was how it worked.  I guess 
the same argument could apply to any script -- maybe they all should have 
two dashes after bash on the first line.  On the other hand, you can type
this on the command line to execute the script:

bash -- -P/execdir

That's good to know because think of how many scripts you have written 
without the double hyphen on line one!  Luckily, we don't name many 
directories that way, so it probably won't be needed often.

Mike