Path: csiph.com!news.uzoreto.com!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!usenet.stanford.edu!not-for-mail From: Andrew Neff Newsgroups: gnu.bash.bug Subject: Re: Bash parameter transforamtion on empty array triggers unset variable. Date: Tue, 11 Aug 2020 11:45:41 -0400 Lines: 72 Approved: bug-bash@gnu.org Message-ID: References: <3ddb8a56-c7d0-b87e-8bb4-520acecc90d5@case.edu> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: usenet.stanford.edu 1597160759 14460 209.51.188.17 (11 Aug 2020 15:45:59 GMT) X-Complaints-To: action@cs.stanford.edu Cc: chet@cwru.edu, bash-testers@cwru.edu, bug-bash@gnu.org To: chet.ramey@case.edu Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=visionsystemsinc-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9PvY6IZn9TtPMvMTebVPT3b6e/D14iMIUT1lDluxyLc=; b=n2db++PS5LGFGAKXvHUxuLdb4MkAezB8sf6DIEK7GId/3GuN8makcMsHV3sN3VxOzr c4q+4LNYfTbiD7pi1eaNb00ZaZiT+rUo/pIgL7374niboLBcXPt7JebWZs7tUB0sRiiT AFve4HuhOq4Iv2ZewlGcigv8GhPrv2o9UHW/G/CM6Bjol4UPZY9842H/qFCefIZe1yie r31dHhfXAuQtm6nQLdENUxmP5ZACMNp1JqtRR1PccWi6QgHasyWDgdcjQR4gXP5S0yVC 26zWIFvq9HsJOAAXdWG4FNqEmayhTTzA5OR36XIbgrTzjLffTtXdecbdrRefqtmNa/YB FVxw== 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=9PvY6IZn9TtPMvMTebVPT3b6e/D14iMIUT1lDluxyLc=; b=tJN9GhnHy1JYB4GbCwyBk7pJB0HNhrCNgNW9rd76bcNsAht5GMSFCpkzc98I+Jbx4k dNKzi0A9E64BccwJjzhLATW4gWJNfi2aqRvEaUpgX5NyBKONVBIOx8uNj3Ka6g2+h0cW 1zs8+yZSifGvMHaqxSIHXKQ5Fz2yC9fOEU+prFLM2ii9Y6PjD6v4M/B+xFV/vZYjsge8 pelMSHefUMYF9QGtq5OVEWplPQcR0ChFVkjSV8eK0/N8Dby8mvMxlUk2O0vxGhjCE8u0 rlLiQscuzP4SJSUiKML/p63oEINlYXupayatUQV3S9E50OSMEshKF6YqgJo+33FvxeJ8 sC7g== X-Gm-Message-State: AOAM532sO4hW8Eb8/4YhI0+QYOhEDndEkFl4J4jgkNylcRTdPzXgg0ms cSsiIV83lzPumZD/uMrMrIYskzRwMusR7EDpWS2wIQ== X-Google-Smtp-Source: ABdhPJzPL3Lw7bMHmIkH3v2K5sJ1ju6SeXwsejuI2/q7MsR6YsRK+DLIH1aRASFUhOVjMAWnC+GpAozDoOSTIp/CerI= X-Received: by 2002:a9d:6a88:: with SMTP id l8mr5294144otq.13.1597160752681; Tue, 11 Aug 2020 08:45:52 -0700 (PDT) In-Reply-To: <3ddb8a56-c7d0-b87e-8bb4-520acecc90d5@case.edu> Received-SPF: none client-ip=2607:f8b0:4864:20::32b; envelope-from=andrew.neff@visionsystemsinc.com; helo=mail-ot1-x32b.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <3ddb8a56-c7d0-b87e-8bb4-520acecc90d5@case.edu> Xref: csiph.com gnu.bash.bug:16740 Ah, I see the confusion. The issue you pointed out, "@Q breaks set -o nounset" refers to quote parameter expansion, as does the line in CHANGES-5.1, 1.q, which makes sense to call this a bug that was allowed in bash 4.4 and 5.0. I should have specified, the focus of this issue is the "@a" expansion. It makes sense that @Q/E/P/A expansion should not work on unset variables with nounset enabled. However, @a is uniquely different, in that it does not have to do with the value of the variable, but rather the variable type. My "set -eu" compatible library uses the @a expansion (for bash 4.4 and newer) because it had previously worked on unset values with set -u enabled, which was a very useful feature. Here are 3 specific details I would like to address: 1. @a expansion should work on unset variables with "set -u" in bash 5.1. It seems like the correct thing to do. Only @a expansion. This has been a very useful feature in bash 4.4 and 5.0. Should fail: (set -eu; declare -a x; echo "${x@Q}") Should not fail: (set -eu; declare -a x; echo "${x@a}") 2. With "set -u", the following works in bash 4.4 or newer (and makes sense that it works): (set -eu; x=(); echo "${x[@]}") Here x is not unset, it is set to an empty array. This expansion make sense with nounset turned on because x is not unset, it is set to () However, this fails: (set -eu; x=(); echo "${x@a}") This is an inconsistent behavior, and it seems to me the ${x@a} should definitely work here, with nounset turns on 3. The same as #2, but for associative arrays Works: (set -eu; declare -A x=(); echo "${x[@]}") Does not work, but should: (set -eu; declare -A x=(); echo "${x@a}") Thanks On Tue, Aug 11, 2020 at 9:51 AM Chet Ramey wrote: > On 8/10/20 5:52 PM, Andrew Neff wrote: > > > Bash Version: 5.1 > > Patch Level: 0 > > Release Status: alpha > > > > Description: > > I do not know if this is related to bash 5.1 erroneously being > > "a little aggressive about skipping over empty strings" mentioned > > in "Declaring arrays with empty string in one line is bugged", but using > > parameter transformation on an empty array, throws an error if "set -u" > is > > turned on. This was not how bash 4.4 and 5.0 worked. This bug makes it > > impossible to check if an empty variable is an array using parameter > > transformation while "set -u" is turned on > > This was a bug in bash-5.0 (and 4.4) and was fixed in early March 2019 as > the result of > > https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00010.html > > It's in CHANGES. > > There are some other changes in how bash displays attributes of unset > variables when `nounset' is not enabled, but unset variables used in word > expansions should trigger an error -- with the usual @/* exceptions -- when > set -u is enabled. > > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer > ``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/ >