Path: csiph.com!goblin3!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: konsolebox Newsgroups: gnu.bash.bug Subject: Re: Error on arithmetic evaluation of `~0`. Date: Sat, 29 Dec 2018 20:09:13 +0800 Lines: 43 Approved: bug-bash@gnu.org Message-ID: References: <25e8adf0-96d5-1e38-faea-e7620ba3f846@case.edu> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Trace: usenet.stanford.edu 1546085719 18518 208.118.235.17 (29 Dec 2018 12:15:19 GMT) X-Complaints-To: action@cs.stanford.edu Cc: Chester Ramey , bug-bash To: Bize Ma 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=93C8STrG3aR10/P8qkAjO8Ho9KL2K1vpwpJAB8clBPU=; b=M51815VjjQfumy5nfehKcycHBPioDqbJSLqp6W5lNpZOnDoNE7pRt/bkLSunq+8roC r0N+DKw55Td4NhH5VOglGFCgag+E28w5OP4LOxaGVgoM6a6i/Y31FX8F1So5+a+AQNsZ PQ0unTLQ69c1nlnt6nH4+M/KdGuOpUilMQdtzitMMtteBP4sTNW4g05dbrebW4ProuwY nRmVDn0H2IsYMM+bjsQeQCMjsnAoJXJ+v9/6qvuqLpz+Ii767nOMyZsQYPeQGwIDmpp4 0PqvGn7EvurKzAEBjdhmTZHG5D+deHumFiuyl/THkN1uYwt5qj7eP3aU4Mi4l7PK6tld W3vg== 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=93C8STrG3aR10/P8qkAjO8Ho9KL2K1vpwpJAB8clBPU=; b=W21JlsfuPXcT4DKGHgBGxsnIVAOOMMCOfSYkrZxJWr8QfSWu/ESa7G1Ne7iAWiK6of E4eFZJIEdcdnIQEXYHpodwlq5LYC8UKsrfz/CW4+X/5h1oZnkhrNGEfLGG3c73yU107R K3JcbO5tp1L+NTx14NebyhTCcC9QIOgK1W2LXtJRXajddPLpNDGn8qblB37cbKdXf6NQ 8yrX+mZiRT3ukTko2ISv5QBe/QhyNNvvanFu4EwyJbd+QeflYwOd3jTsLnolzDRYwou7 oSH6QHZrS50ix30Of69vm/8euOdyJwB22fgthbASbAZ2unc1AIpzzcFtpWWNbz1QfYGe PPAw== X-Gm-Message-State: AJcUukfEECNM7scI+Bk9kdHzXgmUpLBCeC4Rt0UjAC0b8WJc2/37I/Of O+KPyt+HQAzvpxJF7zsjAgYSTsOKoPCUkT9kKq0= X-Google-Smtp-Source: ALg8bN4m5fZXwz2YOWAM31QIUmXFEyPymn3iPDmPwFkK194VnadGtU8qHedUKsNXmZp4rHjU1GQKugL+mCZgHuS28Hs= X-Received: by 2002:a5e:aa06:: with SMTP id s6mr21784348ioe.187.1546085366529; Sat, 29 Dec 2018 04:09:26 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::d31 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:15001 On Sat, Dec 29, 2018, 1:44 PM Bize Ma Chet Ramey () wrote: > > > On 12/23/18 12:01 PM, Bize Ma wrote: > > > {=E2=80=A6} > > > > Both command line above should have printed "hello". > > > > No. 0 is the only valid subscript for a non-array variable. The > difference > > between bash and other shells that implement this feature is that bash > > warns about negative subscripts. > > > > If you say so: fine for me. > > It still irks me a little that a `${var[-1]}` isn't the "last value" > (sometimes!, consistency?). > That consistency would demand that simple variables be treated exactly as array variables, but they aren't. The consistent part here is that they convert dynamically when indexed, and 0 is the only synonymous index if the variable would have been an array. A different index would demand two step conversion. And if bash would ever allow those inidices like the positive ones, it would only be for the sake of convenience, but that shouldn't be taken strictly. If you want consistency, stop treating non-array variables as array variables and write explicit declarations or conversions. Natural indices are fine, but special non-generic ones that are clearly just for arrays are not. > I haven't seen that documented anywhere, though. > Read the source code so you would undestand that arrays and non-arrays are different. >