From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7306 invoked by alias); 3 Nov 2014 09:22:12 -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: 19319 Received: (qmail 23117 invoked from network); 3 Nov 2014 09:22:09 -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=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=xNp/HB0YBZC03I221jmu4aEBo74ZFzFGKZQbL8UP4Pg=; b=b2a07iAB60fzFCG2MSyMytGou6BBKmdh4B9SU/uovqdpu4EztL90mI4yBO+Jw9CoAo Z/2kjMu1KzR3yaA4ehrp4jqWptEY9KKH8kP2HPDJAJxBqFFiVrocxQxU6oBP5p+DgWC+ IdMJ3R3WVcAwNQRKn/E4gyEmsEDTne/JUJb5GQhJOZ8RfWodNZfYL/XHsXQFoBK8YbZP qHQCU8v9wtG8lF9u6IGIGrjlw8ENlSJsI0Pac1bX5hc0lliztL2Xdvl8caaicBV2C+IQ Kwq9JrrsrJab46xclulRGOOzIw2qtnak+v0gpbzl64hTNg1KhRyg8sxx5/q6htHKOID0 o2Ug== MIME-Version: 1.0 X-Received: by 10.107.134.89 with SMTP id i86mr1265368iod.53.1415006523835; Mon, 03 Nov 2014 01:22:03 -0800 (PST) In-Reply-To: <5456E6EA.2050806@eastlink.ca> References: <5456984A.3020001@eastlink.ca> <20141102213713.GA4412@chaz.gmail.com> <5456E6EA.2050806@eastlink.ca> Date: Mon, 3 Nov 2014 10:22:03 +0100 Message-ID: Subject: Re: for loop question From: Mikael Magnusson To: Ray Andrews Cc: Zsh Users Content-Type: text/plain; charset=UTF-8 On Mon, Nov 3, 2014 at 3:22 AM, Ray Andrews wrote: > On 11/02/2014 05:53 PM, Mikael Magnusson wrote: >> >> We might want to avoid using obscure mixes of the discouraged alternate >> syntax with syntax that depends on short_loops being set, when helping >> people who are asking questions about basic syntax. :) > > It does get a bit overwhelming. However, when I poke at the syntax by > asking 'why can't we ...' sorts of questions, I expect to more or less have > my fuses blown ;-) Still, any sort of heads up about what is considered > proper vs. what is considered discouraged will be most appropriate. If you look up the section "Complex Commands" in the manpage, they're all listed fairly well explained. The section after that is called "Alternate Forms for Complex Commands" and lists some syntax that is convenient interactively but not super encouraged. Using any syntax that depends on SHORT_LOOPS being set will of course not work in any other shell, and also has the downside that making a small mistake will usually still parse but do something entirely unexpected. I'm a bit more opposed to SHORT_LOOPS than others here, but when I disabled the option, I immediately found like 5 bugs in the completion system so I think I'm at least somewhat justified :). The whole point of it is just to not have to write do; done, but it causes so many bugs and confusion. As to the for loop, it has two different forms. They're almost so different you could say the only thing they have in common is the word "for" :). One is for some variables in list of values; do list; of; commands using $some $variables done Think of that as a foreach loop. The other form is for (( i=0; i<10; i++ )); do list; of; commands using $i done This form is exclusively for math expressions, you can't just stick arbitrary commands in there. If you want that, just use a while loop. commands to initialize state while my condition; do stuff here increment iterator or whatever done because as you saw, stuffing it all into the for line would just get really ugly. And you want the foreach form for your initial example as Stephane said, there is no need to iterate over the index unless you have a veeery large array, and then it will already be very slow anyway. As a side note, if you ever want a do-while loop instead of a regular while loop, the trick is while just put the whole body; of the loop here instead; and the last command is; the terminating condition; do done -- Mikael Magnusson