bug-bash
[Top][All Lists]
Advanced

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

Re: Curious case of arithmetic expansion


From: Eduardo Bustamante
Subject: Re: Curious case of arithmetic expansion
Date: Sun, 23 Apr 2017 18:26:26 -0500

On Sun, Apr 23, 2017 at 3:25 PM, Florian Mayer <address@hidden> wrote:
[...]
> Why not? Why is it not reasonable to expect an intuitive
> result from (())? The most intuitive thing, in my opinion,
> would be to use nameref for side effects by default, because in order
> to get a value from an id, (()) already follows namerefs.

First, the documented behavior
(https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html#Shell-Arithmetic)
is this:

| The *value* of a variable is evaluated as an arithmetic expression
when it is referenced,
| or when a variable which has been given the integer attribute using
‘declare -i’ is
| assigned a value

(emphasis is mine)

Second, every other major shell that implements recursive evaluation
of arithmetical expressions behaves the same way as bash:

| address@hidden:~$ for sh in dash mksh zsh ksh93 posh 'busybox sh'
bash; do $sh -c 'a=b b=2; echo $((a++)) $a $b'; done
| dash: 1: Illegal number: b
| 2 3 2
| 2 3 2
| 2 3 2
| 2 3 2
| 2 3 2
| 2 3 2
|
| address@hidden:~$ dpkg -l dash mksh zsh ksh posh busybox bash
| Desired=Unknown/Install/Remove/Purge/Hold
| | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
| |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
| ||/ Name                        Version            Architecture
 Description
| 
+++-===========================-==================-==================-============================================================
| ii  bash                        4.4-4+b1           amd64
 GNU Bourne Again SHell
| ii  busybox                     1:1.22.0-19+b2     amd64
 Tiny utilities for small and embedded systems
| ii  dash                        0.5.8-2.4          amd64
 POSIX-compliant shell
| ii  ksh                         93u+20120801-2+b1  amd64
 Real, AT&T version of the Korn shell
| ii  mksh                        54-2+b2            amd64
 MirBSD Korn Shell
| ii  posh                        0.12.6+b1          amd64
 Policy-compliant Ordinary SHell
| ii  zsh                         5.3.1-4            amd64
 shell with lots of features

And third, you haven't really made a compelling argument to change
this behavior. Doing so will break backwards compatibility,
compatibility with other shells that implement this feature, and in my
opinion, doesn't help much in making recursive expansion of
arithmetical expressions more "intuitive".

Why is it so important that this be changed?



reply via email to

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