Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Passing arguments to anonymous functions
- X-seq: zsh-users 15575
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Passing arguments to anonymous functions
- Date: Wed, 24 Nov 2010 01:22:50 -0800
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
Zsh has had anonymous functions for a couple of years now:
% (){ print hello world }
hello world
%
These are useful for things like creating local variable scopes, which
you can't do with a simple { ... } expression. However, $@ and $* are
always empty in an anonymous function, which makes them somewhat less
useful than a named function which can be called with arguments.
However, you can redirect the input of an anonymous function, and you
can read into the positional parameters:
% (){ read -A argv; print -l $@ } <<<"goodnight gracie"
goodnight
gracie
%
This isn't quite as nice as one might like since one must supply a
single shell word to follow the <<< redirection, but for simple cases
it does suffice, and one can imagine all sorts of tricks involving e.g.
IFS=$'\0' (if one does not use the new POSIX_STRINGS option) to allow
parsing of arguments passed from caller to callee.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author