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

Re: for loop question



On 11/03/2014 06:43 PM, Bart Schaefer wrote:
The 0 is appended because [ -n "$TLC[i]" ] does not produce any output,
But in

      if [ -n "$TLC[i]" ]

... the test must surely produce an answer of some sort. Yes or no. If that answer is not
acceptable as 'arithmetic', then what is it?

   ! $([ -n "$TLC[i]" ]; echo $?)

'echo $?' seems to receive a one or a zero, and that's 'arithmetic', so
why does

    ! $([ -n "$TLC[i]" ]);

... not do exactly the same thing? Why do we need 'echo' to cough up
a testable number?  What does the '$?' do but carry the return  value of
the test? And what would break if the test just returned it's own
return value with no need for help from 'echo $?' to restate it? When we use
the 'if' form above, we don't need the help of 'echo $?' so why do
we need it inside 'for (('? It seems like a pointless limitation.
Maybe a trivial limitation, but still pointless. Indeed:

   [ -n "$TLC[i]" ] && echo "Yup, there's something in there"
... so the [] test doesn't even need 'if' to work, it *does* produce
a testable return value, and IMHO that's 'arithmetic', so it should
work in 'for (('.

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.

Sure, but we get used to using '!' to reverse true/false, no? You hafta wonder, tho, how that convention ever got started. '0' is 'false', yet functions return '0' on success. AFAIK it has
always been like that.



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