current dir from .command scripts

Andy Lee aglee at mac.com
Sat Aug 4 07:04:35 PDT 2007


$0, $1, $2 etc. is replaced by the shell with the nth argument to the  
current command.  The zeroth argument, $0, is the command name  
itself, which is bash when you enter commands in your Terminal window.

If you do "echo $0" at the shell, you get "-bash".  (I don't know why  
it's that instead of "/bin/bash".)  So "dirname $0" expands to  
"dirname -bash".  Putting backticks around "dirname $0" causes  
"dirname -bash" to be evaluated by the shell.  The shell thinks you  
are trying to pass a -b option to dirname, which it doesn't support.

If you were to include "echo `dirname $0`" in a *script*, it would  
succeed, because the "zeroth" argument to the script is the path to  
the script file.

--Andy

On Aug 4, 2007, at 1:57 AM, Hex Star wrote:

>
>
> On 8/3/07, Scot Hacker <shacker at birdhouse.org> wrote:
> On Aug 3, 2007, at 11:54 AM, Mike Galloy wrote:
> > echo `dirname $0`
>
> Bingo!  That nails it. Thanks heaps, Mike Galloy.
>
> Scot
>
>
> Is something wrong with my syntax and if so what? Thanks! :)
>
> Last login: Fri Aug  3 22:10:25 on ttyp1
> Welcome to Darwin!
> macminis-computer:~ hexstar$ echo `dirname $0`
> dirname: illegal option -- b
> usage: dirname path
> _______________________________________________
> MacOSX-talk mailing list
> MacOSX-talk at omnigroup.com
> http://www.omnigroup.com/mailman/listinfo/macosx-talk



More information about the MacOSX-talk mailing list