Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: retrieving invocation arguments



On Sep 14,  9:42pm, Adam Spiers wrote:
}
} What's the best way of retrieving the arguments with which zsh was
} invoked?

By examining $0, $-, and $*.  This is imperfect; $- doesn't tell you
what options were turned off.  However, in a non-interactive shell,
the only interesting option that is on by default is BG_NICE (-6).  It
is also not possible to get the command argument to -c, and when -c is
given the first non-option argument after the command to -c becomes $0
rather than being included in $*.  That is,

    zsh -fc 'echo $0' foo

will print "foo".

The best way to detect whether -c was given appears to be

    [[ -o shinstdin && ! -o interactive ]]

and to find out whether $0 is really an argument following the command,

    [[ $# -gt 0 || $0:t != $ZSH_NAME ]]

Of course you can fool that, e.g.

    zsh -c '[[ $# -gt 0 || $0:t != $ZSH_NAME ]] && echo Got $0' zsh

won't print anything, but that's a pretty obscure case.

} It doesn't appear to be stored in any parameter set by the shell.

AFAIK this is true of all shells.

} Worth doing?

It wouldn't be particularly difficult, but what's the application you
have in mind?

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



Messages sorted by: Reverse Date, Date, Thread, Author