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

Re: for loop question



On Mon, Nov 03, 2014 at 06:43:38PM -0800, Bart Schaefer wrote:
> } 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.

Cool! Thanks a lot!



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