Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #14944
| Path | csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail |
|---|---|
| From | Bize Ma <binaryzebra@gmail.com> |
| Newsgroups | gnu.bash.bug |
| Subject | Re: Error on arithmetic evaluation of `~0`. |
| Date | Sun, 23 Dec 2018 13:01:47 -0400 |
| Lines | 73 |
| Approved | bug-bash@gnu.org |
| Message-ID | <mailman.6207.1545584526.1284.bug-bash@gnu.org> (permalink) |
| References | <CAFra36gW8Csu-84oRLV7QsR7PbiMYq6g7WnMktjfnrSdTys38g@mail.gmail.com> <25e8adf0-96d5-1e38-faea-e7620ba3f846@case.edu> |
| NNTP-Posting-Host | lists.gnu.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| X-Trace | usenet.stanford.edu 1545584527 29473 208.118.235.17 (23 Dec 2018 17:02:07 GMT) |
| X-Complaints-To | action@cs.stanford.edu |
| Cc | bug-bash <bug-bash@gnu.org> |
| To | Chester Ramey <chet.ramey@case.edu> |
| Envelope-to | bug-bash@gnu.org |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=YUriCrJ3561g0mmpNkihDHBN0VO/W79iTedM09xJ+zE=; b=MSObymq5wdZCUrNMqWMk/d02lKqePfX+RWzkr5sSVj5R7h1TS5E9GCKRgw9orN4Jrc BvL8HFbEsdkdTpOTB0D2+Jl3Oi/pUSBDZY5/CuAd80eLN890tic+ZCl8qk21+jh2kj5o n+ILZxbJO1FRetgIUvuqzK9FnW7EhP7u10/mXUzUS8u8reCf8kWwzbkOk/7wV0gSz2Hz q26VKFDhsV93bxTblpzvAVMmxtCkzOnezFBU7nfYr5HMyT4QNfIK8Ljx1XwD3RB2x3Ok KYnvbzaTmbJuVdv5Q0cZFqs6/OAw2w8m8cVOa3TNcVC/MzcN3kglZixm8DzdnxN6FT0X XJSw== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=YUriCrJ3561g0mmpNkihDHBN0VO/W79iTedM09xJ+zE=; b=bJ3jGOa2OG3PcX98hqR8nhIuv9NICRlahp6/YHS+5n996AK0e2Y0+9NBPgFYs2jrdS mhtHr2dItHDbcu4EPJznF+nF84cRij2WareOiULhyCpH9Qt2KtM1n09u/fkBgv2E7Xub HBj1NVX3tMJCtX2Ylcm6BOB0RO+Gu5IE6iaT586UD9y/ujCDw6b7CqlAQvZ/H7ORS5UC HxIo7etLoWPkY9h1IDGJFIULqBpXJHbKJ5aNj6bms6z27lXGZ415Zo78RgV5LnmQ+adG MZMnEppdJ5/vcxIfaCeyAi3J7wEUTTt4KEPyREzBxdjUQPS7tcK323aVIHK+DGrp+TRi QNGg== |
| X-Gm-Message-State | AJcUukcwSQkXGZqSghmILhbEA3ewsdonK6ipBZkxljvKUqvqfZSWO0pG /7kstv+qFBxro2ac0SFTgt1iq+34ycHLlcbd/5B1uw== |
| X-Google-Smtp-Source | ALg8bN6sHcbgmSbUKmNHxLkXv8Jz+Q9PgjD6M/YUh3poDwKThQdBEFNGYfbuqJdBQQ9qQ3XJfVslIyIBupGS9j1Id0s= |
| X-Received | by 2002:a9d:2aa5:: with SMTP id e34mr6393489otb.67.1545584520751; Sun, 23 Dec 2018 09:02:00 -0800 (PST) |
| In-Reply-To | <25e8adf0-96d5-1e38-faea-e7620ba3f846@case.edu> |
| X-detected-operating-system | by eggs.gnu.org: Genre and OS details not recognized. |
| X-Received-From | 2607:f8b0:4864:20::32a |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.21 |
| X-BeenThere | bug-bash@gnu.org |
| X-Mailman-Version | 2.1.21 |
| 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:14944 |
Show key headers only | View raw
Chet Ramey (<chet.ramey@case.edu>) wrote:
> >
> > While this works:
> >
> > var=(hello); echo "${var[ ~0]}"
> > hello
>
> Because negative array subscripts count backwards from the end of the
> array.
>
Doh!, yes. And, because of that: "${var[-1]}"
should give the *last* element of array "var", shouldn't it?
Consequently, this happens:
$ unset var; var[0]=77; echo "${var[0]}"; echo "${var[-1]}"
77
77
The only value in var is at index 0, which means it is also the *last*
value.
> > It is also interesting that this fails:
> >
> > var=hello; echo "${var[ ~0]}"
> > bash: var: bad array subscript
> >
> > Isn't `var[0]` valid and equivalent to `var` ?
>
> Yes, but ~0 (-1) is not the same as 0.
>
Doh!, yes, of course, "0" is not equal to "~0" (-1). But if you were to
compare the two last command lines *as posted:*
> var=(hello); echo "${var[ ~0]}"
> var=hello ; echo "${var[ ~0]}"
You will notice that the only difference is that one explicitly creates an
array (and works) while the other creates an scalar (and fails).
The point being that a variable which has an scalar value "var=hello"
should act (for most practical cases) as an array for which only the
value at address 0 has been defined.
Both command line above should have printed "hello".
But it should be more clear to you written as this:
unset var; var=(hello); echo "${var[0]}:${var[ -1]}"
unset var; var=hello ; echo "${var[0]}:${var[ -1]}"
The first line works, the second fails on the negative index.
It seems that bash asserts that the variable is an array when the
index is negative (and emits an error if var is not an array).
> > This was "supposed" to be resolved in a dev version,
> > but is still present on bash 5.
>
> The other arithmetic contexts you reported (the "pure" arithmetic contexts
> the comment above references) were changed; this was left for backwards
> compatibility. Like I said above, it looks like it's time to deemphasize
> that.
>
In other words: you solved this for *some* arithmetic contexts, but not all.
Thanks for the confirmation.
Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread
Re: Error on arithmetic evaluation of `~0`. Bize Ma <binaryzebra@gmail.com> - 2018-12-23 13:01 -0400
csiph-web