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

Re: N=5 echo $[N-1] why doesn't it work as expected



Mikael Magnusson wrote:
> I thought of anonymous functions here and tried
> N=5 () { echo $((N-1)) }
> but I get no result, nothing is printed, and I don't seem to get a
> function definition for any of "N", "N=5" or "5", and no error is
> returned. It also seems to be illegal to put the expression in front
> of a loop of some sort or a subshell or list, ie, N=5 for ... done,
> N=5 { ... }, N=5 ( ... ), but at least those print "zsh: parse error
> near `foo'".
> 
> If I'm not blind, it looks like zshparam(1) doesn't talk about this at
> all and it's sort of hard to grep for; where is it documented?

It's in grammar.yo (the start of zshmisc(1)): the assignment only works
when it's at the front of a "simple command", as defined there.  So it
won't work in the case of a function definition, although there probably
ought to be an error.  However, this isn't to do with anonymous
functions:

% N=5 fn() { echo $N; }
% which fn
fn not found


A em(simple command) is a sequence of optional parameter
assignments followed by blank-separated words,
with optional redirections interspersed.
The first word is the command to be executed, and the remaining
words, if any, are arguments to the command.
If a command name is given, the parameter assignments modify
the environment of the command when it is executed.
The value of a simple command is its exit status,
or 128 plus the signal number if terminated by a signal.
For example,

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


'member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom'



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