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

Re: Airthmetic confusion...



In the last episode (May 16), Mike Hernandez said:
> If you set a variable to any integer other than 0, positive or
> negative, the return code is 0, which is successful exit, for example:
> 
> (mike@mhernandez)(30/pts)(04:53pm:05/16/05)-
> (%:~)- (( r = -5 ))
> (mike@mhernandez)(31/pts)(04:53pm:05/16/05)-
> (%:~)- echo $?
> 0
> 
> This occurs with a regular assignment as above, or if the assignment
> contains some variables (as in x = b - 4, if b was 4 the return code
> would be 1, yet the math is done and x is set to equal 0).
> 
> There must be a reason why assigning 0 to a variable is considered an
> error (a reason which I could speculate about, but don't know for
> sure).

That's because within an arithmentic context, 0 is false and 1 is true,
so the following are equivalent:

true && echo true
(( 1 )) && echo true
(( 2 > 1 )) && echo true   # since $(( 2 > 1 )) evaluates to 1

An assignment expression evaluates to the value of the variable, so 
(( a=0 )) is the same as (( 0 )) .

-- 
	Dan Nelson
	dnelson@xxxxxxxxxxxxxxx



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