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: Fri, 28 Dec 2018 03:33:30 +0800 Lines: 82 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" X-Trace: usenet.stanford.edu 1545939229 404 208.118.235.17 (27 Dec 2018 19:33:49 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=QF67m5wgT9SIPq6hc6l8edwUuLFkEAD/2hhtJQX6rD0=; b=Ch3t+A/VckYe0D+rZTJW/9iOgTSsHejJX49Ni8zmfMXN6df6V+T9gA+QHJauNSwuUy IWeGkqRSVSa1X0t0z2lnnP/MUYTjbld9kAGqCD8vu7JGmzey8YHU82a2fUoCXCIBc1xQ 7Msl9LboWK9uFcMH0VLirEK/pB3tbQMHAQHLqpMbsfx0rZfRNlZDRKxDYiNm74XYG6/W xj4fnr+Bsw6v6KSFcWVTsY5A1dEeuOWrgZtzDtmuhd7+V4gbVQghM4PoVxpWu/QMnY5d jzaw4qMQlO56ulfFPws9/Q8S+RML4XokPrGk0G1SZ0/mrr6nhYiedTQBQ/vK5Xt/BOM8 C98A== 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=QF67m5wgT9SIPq6hc6l8edwUuLFkEAD/2hhtJQX6rD0=; b=k0zqOcr+sDRRNwPhatIKHzgotJu6L7sZGHpXUYQwEyLsp2Ypqer8v4zokD2lGSYQdF yjSUwoZ49nwIAsFFB8rSeDXp7R+3ubYcea+x0cQdVr7hVOChdR9gBoUwMPBEwn2fxlQ/ qTT/dmDyUNTY4++OnyiV2Kn3KtH+2XFRiOMjyN4w2Is0znIoaA63QQwSCf57FPygN8ka 1Gqd7RytHQLC9+aalwjPnvRSqKaUX3i0SNLGAwwrdqNLXLwKuH6blwsMiHX7eglmVExq UTxjYKg0zL/0tkRvHxtvXBQqQQsfobkXaByttXtI6WBekUw/reaO3xx/Ca1S77VgIVu4 aJpQ== X-Gm-Message-State: AJcUukfvAu2F+3ZlK+chnRmfcbGdk+74VPciDbhe3p17usK2+eUhE2Zr Ll9GFpnnyXgfgVtpmluMQJAidaJe1HKNReJHVT0= X-Google-Smtp-Source: ALg8bN4yn/DWoYU0E+/dQQZbc27j+v5Px797Kh0puXEAHl9tvLoCrABGXA03sprksQNYz7tcmQCCdHBhGdduwF2Qe2M= X-Received: by 2002:a5d:870c:: with SMTP id u12mr17032073iom.168.1545939222791; Thu, 27 Dec 2018 11:33:42 -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:14970 Simple variables convert to array variables dynamically, but that doesn't mean they should be interpreted exactly as if they are. I see that more of just a convenient feature. On Mon, Dec 24, 2018, 1:02 AM Bize Ma Chet Ramey () 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. >