[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash 'let' can give error

From: Ken Irving
Subject: Re: bash 'let' can give error
Date: Fri, 10 Dec 2010 06:24:09 -0900
User-agent: Mutt/1.5.20 (2009-06-14)

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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]