[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Inconsistent arithmetic evaluation of parameters
From: |
Greg Wooledge |
Subject: |
Re: Inconsistent arithmetic evaluation of parameters |
Date: |
Tue, 1 Sep 2015 13:50:30 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, Sep 01, 2015 at 12:50:23AM -0400, Clint Hepner wrote:
> Repeat-By:
>
> foo=bar
> bar=5
> echo $(( foo )) # produces 5
> echo $(( foo++ )) # produces 5
> echo $foo # produces 6, not bar
> echo $bar # produces 5, not 6
bar was never changed from its value of 5, so I would say the final
result is correct.
The $(( foo++ )) part is questionable. You've asked bash to increment
a variable (in a math context), but that variable doesn't have an
integer value. But it "points to" (contains the name of) a variable
that does.
I would say that any of the following would make sense:
1) An error message is printed.
2) foo is unchanged (still contains "bar"), and bar is incremented.
3) foo takes on the value of bar, and is then incremented.
Bash happens to do #3.
If you think of $(( foo++ )) as being basically equivalent to
$(( tmp=foo, foo=foo+1, tmp )) then #3 is actually quite sensible.
- Inconsistent arithmetic evaluation of parameters, Clint Hepner, 2015/09/01
- Re: Inconsistent arithmetic evaluation of parameters,
Greg Wooledge <=
- Re: Inconsistent arithmetic evaluation of parameters, Dennis Williamson, 2015/09/01
- Re: Inconsistent arithmetic evaluation of parameters, Greg Wooledge, 2015/09/01
- Re: Inconsistent arithmetic evaluation of parameters, Dennis Williamson, 2015/09/01
- Re: Inconsistent arithmetic evaluation of parameters, Andreas Schwab, 2015/09/01
- Re: Inconsistent arithmetic evaluation of parameters, Dennis Williamson, 2015/09/01
- Re: Inconsistent arithmetic evaluation of parameters, Andreas Schwab, 2015/09/02
- Re: Inconsistent arithmetic evaluation of parameters, Dennis Williamson, 2015/09/02
- Re: Inconsistent arithmetic evaluation of parameters, Greg Wooledge, 2015/09/02
- Re: Inconsistent arithmetic evaluation of parameters, Chet Ramey, 2015/09/02
- Re: Inconsistent arithmetic evaluation of parameters, Greg Wooledge, 2015/09/02