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


Groups > gnu.bash.bug > #11477

Re: Inconsistent arithmetic evaluation of parameters

Path csiph.com!xmission!news.glorb.com!usenet.stanford.edu!not-for-mail
From Stephane Chazelas <stephane.chazelas@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Inconsistent arithmetic evaluation of parameters
Date Thu, 3 Sep 2015 14:45:29 +0100
Lines 56
Approved bug-bash@gnu.org
Message-ID <mailman.450.1441291925.19560.bug-bash@gnu.org> (permalink)
References <61895190-83A6-4D62-B90E-65023D5E966B@gmail.com> <20150901175030.GP4309@eeg.ccf.org> <CANaoh6LNi_YyOOB56ZMR3P8thRSMfoTcDCGyW8S921nVoP1H7Q@mail.gmail.com> <20150901202308.GR4309@eeg.ccf.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1441291926 17873 208.118.235.17 (3 Sep 2015 14:52:06 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 05448dab.skybroadband.com
Content-Disposition inline
In-Reply-To <20150901202308.GR4309@eeg.ccf.org>
User-Agent Mutt/1.5.21 (2010-09-15)
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 80.91.229.3
X-Mailman-Approved-At Thu, 03 Sep 2015 10:52:04 -0400
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.14
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:11477

Show key headers only | View raw


2015-09-01 16:23:08 -0400, Greg Wooledge:
> On Tue, Sep 01, 2015 at 03:13:57PM -0500, Dennis Williamson wrote:
> > The version of dash I have handy (0.5.7) has math support which IMHO is
> > broken:
> > 
> > $ foo=bar
> > $ bar=5
> > $ echo $foo
> > bar
> > $ echo $((foo))
> > dash: 4: Illegal number: bar
> > $ echo $(($foo))
> > 5
> > $ echo $((bar))
> > 5
> > $ echo $(($bar))
> > 5
> > 
> > Note the inconsistency in support of omitting the inner dollar sign.
> 
> $foo is expanded to bar, so the following two lines are always going to
> be equivalent:
> 
> echo $(($foo))
> echo $((bar))
> 
> POSIX also specifies (vaguely!!) that $((x)) and $(($x)) are equivalent.

Note that while POSIX may (vaguely indeed) say $((x)) and
$(($x)) are equivalent at least when x contains a litteral
number, $((-x)) and $((-$x)) are not equivalent in all shells if
$x contains a negative number.

$ a=0 x=-1 bash -c 'echo $((a-$x))'
bash: a--1: syntax error in expression (error token is "1")
$ a=0 x=-1 bash -c 'echo $((a-x))'
1

(they're OK in shells that don't implement the (optional in
posix) -- and ++ operators like dash.

Or you can add spaces to make it more reliable: $((a - $x))

In ksh/zsh/bash, see also the difference between:

$ a=1+1; echo $((a*2))
4
$ a=1+1; echo $(($a*2))
3

-- 
Stephane
> 
> 

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


Thread

Re: Inconsistent arithmetic evaluation of parameters Stephane Chazelas <stephane.chazelas@gmail.com> - 2015-09-03 14:45 +0100

csiph-web