Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11452

Re: Inconsistent arithmetic evaluation of parameters

From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: Inconsistent arithmetic evaluation of parameters
Date 2015-09-01 13:50 -0400
Message-ID <mailman.347.1441129866.19560.bug-bash@gnu.org> (permalink)
References <61895190-83A6-4D62-B90E-65023D5E966B@gmail.com>

Show all headers | View raw


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.

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Inconsistent arithmetic evaluation of parameters Greg Wooledge <wooledg@eeg.ccf.org> - 2015-09-01 13:50 -0400

csiph-web