From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2512 invoked by alias); 12 Feb 2014 06:39:26 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18425 Received: (qmail 1672 invoked from network); 12 Feb 2014 06:39:19 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 From: Bart Schaefer Message-id: <140211223909.ZM24888@torch.brasslantern.com> Date: Tue, 11 Feb 2014 22:39:09 -0800 X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Mildly interesting zargs trick: call an anonymous function MIME-version: 1.0 Content-type: text/plain; charset=us-ascii Leonardo Barbosa's question got me thinking: What if the number of files is so large that "rm" chokes with "argument list too long" or the like? Then you want to use zargs to split the list into managable chunks: zargs -- **/*.tex(.:s/.tex/.aux) -- rm However, there's only so much stuff you can pack into the recursive glob before it begins to get unweildy. So if it were more complicated than this example, you could define a function to do the heavy work. rm_aux() { rm ${*:s/.tex/.aux} } zargs -- **/*.tex(.) -- rm_aux That lets us use the array tricks without creating a global array, but leaves the function rm_aux hanging around. Wouldn't it be nice to use one of those "anonymous" functions supported by recent zsh? ## DO NOT DO THIS: zargs -- **/*.tex(.) -- (){ rm ${*:s/.tex/.aux} } Oops, we've just defined zargs, --, and all our .tex file names to be functions that execute "rm". That's no good, we have to quote the anonymous function. ## THIS SPEWS "command not found": zargs -- **/*.tex(.) -- '(){ rm ${*:s/.tex/.aux} }' ## BUT DO NOT DO THIS EITHER: zargs -- **/*.tex(.) -- eval '(){ rm ${*:s/.tex/.aux} }' That seems fine, but if any of the .tex files has special characters or whitespace in its path name, that "eval" may do horrible things. Fortunately, there's a simple glob qualifier that will quote all of that stuff for us. ## I AM PRETTY SURE THIS ONE IS OK, BUT NO WARRANTY: zargs -- **/*.tex(.:q) -- eval '(){ rm ${*:s/.tex/.aux} }' There are of course other ways to slice up this particular example. zargs -- **/*.tex(.:rq) -- eval '(){ rm ${^*}.aux }' Other variations left as exercises for the reader.