help-bash
[Top][All Lists]
Advanced

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

Re: is it really required that arr['1']=bla gives syntax error instead o


From: Greg Wooledge
Subject: Re: is it really required that arr['1']=bla gives syntax error instead of parse '1' as 1
Date: Mon, 19 Jul 2021 14:00:18 -0400

On Mon, Jul 19, 2021 at 01:45:10PM -0400, Lawrence Velázquez wrote:
> On Mon, Jul 19, 2021, at 12:45 PM, Greg Wooledge wrote:
> > 
> > > On Mon, Jul 19, 2021, 17:57 Greg Wooledge <greg@wooledge.org> wrote:
> > > > In <https://lists.gnu.org/archive/html/bug-bash/2021-02/msg00189.html>
> > > > Chet says that "The contents of (( are expanded as if they are between
> > > > double quotes".
> > 
> > On Mon, Jul 19, 2021 at 06:27:28PM +0200, Alex fxmbsw7 Ratchev wrote:
> > > (( "2" + 4 ))
> > > noerr
> > 
> > Yes.  Double quotes work.  Single quotes don't.  I believe this is
> > because of what Chet said about the contents of (( being treated as
> > if they're inside double quotes already.
> 
> The man page says that "[t]he expression is treated as if it were
> within double quotes, but a double quote inside the parentheses is
> not treated specially." Presumably that means that double quotes
> are removed as usual.  POSIX contains similar language.
> 
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04

I'm honestly not sure how to interpret that wording.  Apparently shell
authors aren't either, because they all seem to disagree.

unicorn:~$ dash
$ echo $(('1' + 2))
dash: 1: arithmetic expression: expecting primary: "'1' + 2"
$ echo $(("1" + 2))
dash: 2: arithmetic expression: expecting primary: ""1" + 2"
$ echo $(("1 + 2"))
dash: 3: arithmetic expression: expecting primary: ""1 + 2""
$ echo $((""1 + 2))
dash: 5: arithmetic expression: expecting primary: """1 + 2"

unicorn:~$ ksh
$ echo $(('1' + 2))
51
$ echo $(("1" + 2))
3
$ echo $(("1 + 2"))
3
$ echo $((""1 + 2))
3

unicorn:~$ bash
unicorn:~$ echo $(('1' + 2))
bash: '1' + 2: syntax error: operand expected (error token is "'1' + 2")
unicorn:~$ echo $(("1" + 2))
3
unicorn:~$ echo $(("1 + 2"))
3
unicorn:~$ echo $((""1 + 2))
3

Ksh officially wins "most surprising result" for that 51.  I guess I
can see how they got it, but I certainly did not *expect* it.  Nor do I
care to guess whether it satisfies the POSIX wording.



reply via email to

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