Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: for loop question
- X-seq: zsh-users 19323
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: for loop question
- Date: Mon, 03 Nov 2014 18:43:38 -0800
- In-reply-to: <20141104015639.GA2871@localhost.localdomain>
- 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
- References: <5456984A.3020001@eastlink.ca> <CAH+w=7aWS0xyS4CXRJBphDjesfUFQOsyJRMaG3RZRxmuj7xkOg__20885.3257158355$1414962125$gmane$org@mail.gmail.com> <20141102213713.GA4412@chaz.gmail.com> <20141104015639.GA2871@localhost.localdomain>
On Nov 4, 9:56am, Han Pingtian wrote:
}
} > for ((i=1; (z[$([ -n "$TLC[i]" ])0]),$? == 0; i++))
} > print -ru2 -- $TLC[i]
} >
} What does the (z[$([ -n "$TLC[i]" ])0]) mean, please?
It's an overly-obfuscated way to write $([ -n "$TLC[i]" ]). The real
work is the ",$?" part.
z[...],$? is used to throw away the z[...] part and keep the value of $?.
The value of $? comes from the side-effect of the $(...) inside the
subscript of z[...].
The 0 is appended because [ -n "$TLC[i]" ] does not produce any output,
but there has to be a valid number as the subscript, hence z[0].
This could be written a LOT more plainly as
for ((i=1; $([ -n "$TLC[i]" ]; echo $?) == 0; i++))
without needing any more characters. If we're playing golf,
for ((i=1; ! $([ "$TLC[i]" ]; echo $?); i++))
works too.
Which, by the way, points out another reason you can't just use the
exit status of a command in a math context: command success/failure
are the reverse of true/false when taken as integer values.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author