On Thu, Dec 09, 2010 at 05:52:49PM +0000, Dominic Raferd wrote:
> $ val=0; let val++; echo $val,$?; unset val
> 1,1
>
> see the error code 1. Setting any other start value (except
> undefined) for val does not produce this error, the problem occurs
> for let val++ and let val-- if the start value is 0.
>
> for let ++val and let --val the problem occurs if the result is 0.
> Also for the
> command:
>
> $ val=10; let val=val+2*2-14; echo $val,$?; unset val
>
> ...
> Why does this happen? Is it 'by design'? It makes arithmetic with
> bash let very dangerous because it can throw unexpected errors (and
> break scripts running with set -e).
I don't know why this is done, but the behavior is clearly documented
in the manpage:
let arg [arg ...]
Each arg is an arithmetic expression to be evaluated (see ARITH-
METIC EVALUATION above). If the last arg evaluates to 0, let
returns 1; 0 is returned otherwise.
Ken