current dir from .command scripts
Hacker Scot
shacker at birdhouse.org
Sat Aug 4 08:07:28 PDT 2007
On Aug 4, 2007, at 7:04 AM, Andy Lee wrote:
> $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.
Interestingly, the
echo `dirname $0`
trick gives the full working path for .command scripts that are
double-clicked, but just "." (current dir) for the same script run
from the shell. My script is designed to be double clicked, so this
largely solved it for me, but just in case, I'll have to build in
some logic to determine whether it's being double-clicked or run from
the shell, using pwd to get the path if dirname $0 evaluates to "."
Funny business.
./s
More information about the MacOSX-talk
mailing list