Path: csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: =?UTF-8?B?T8SfdXo=?= Newsgroups: gnu.bash.bug Subject: Re: Incorrect / Inconsistent behavior with nameref assignments in functions Date: Fri, 28 Aug 2020 18:37:00 +0300 Lines: 66 Approved: bug-bash@gnu.org Message-ID: References: <20200828152846.GI931@eeg.ccf.org> 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 1598629027 6484 209.51.188.17 (28 Aug 2020 15:37:07 GMT) X-Complaints-To: action@cs.stanford.edu To: "bug-bash@gnu.org" Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=Nax1rzsxk2dVm9T0VfrlbnpoXhzgMdEMGWteF0/co6o=; b=YGxXT7TZjj0HfRwIvhHx5ZjcgWF7gelIHTRNWTS3282dgS+S+p6yzC1w8kwXeb47af bnSzXtdp75VVU3MAWAiulKyQMOqKErx/BxwdHcT696A07RCxEYCF0ghEkQtclYuqeGp8 WTvP11E+6xfO1JnLgF/S1UM/d0lsHMg65M03sBlC6gQbSBs6/Hifqv6TJg5CbiEi/XC9 UIu9b/O9T6ni50vSuqApk1PARhJwN48oggiSnvUTCHfE5Vo3v/0OSL8OU+jS4gwFzehP JZqNlnob3/jGLV1Vr+uASrXlFsXoGPLSIFRa1FQ05f/gRIY0B5JjgN1JIR43wuHoXvb6 W+uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=Nax1rzsxk2dVm9T0VfrlbnpoXhzgMdEMGWteF0/co6o=; b=IRQn5TTbwkZuunBJ5L/iy/20lgkEcEZMvmf6u7+2v25724YLt6HRCTA8Mf3OcDNdd3 qDb42PaZy0kKW6MNr6gpvqVzGBA4buAMz9nYIl997SXIlUhOdS8Jw6A5IQzFB47oYUqk PBnzTHbyHTdK6+5v+kXuxYprZh8x5kUVBKheLa0JH7Whq6XkGpj7INA0Tb5gzWdhPHhA n3r/qMnFSwb/X1kxRUs4efRDxiX1iHM5IdoG086DVBKKjy6zK+Y1APAWo9kBBKE5CJdU hw9mbHoi/DwsPOLA/wRjgR96ZcS3lUR1Bj1m0N+0DlqpJm/FM1Zd4VoBN/+nOLRrBSbA ozUA== X-Gm-Message-State: AOAM532Z+bHBN/wg6xmVMCSdmBzMoBmwnHwjtLHDvImP0F8Weh1WNYCw HQT+1+OJ7pEAAcKiWBsiG3IxPW86t9RosI6X/E04mDj9aJk= X-Google-Smtp-Source: ABdhPJzyAmn4jRv7skpJmzkmneRmidF5/PdCPIMSAgj6dvT5qKgiiz+MVbLtVXXT/mVeIiN9xWYT7ZKly++AUdYfXSU= X-Received: by 2002:a0c:99c8:: with SMTP id y8mr2281939qve.57.1598629021123; Fri, 28 Aug 2020 08:37:01 -0700 (PDT) In-Reply-To: <20200828152846.GI931@eeg.ccf.org> Received-SPF: pass client-ip=2607:f8b0:4864:20::f2c; envelope-from=oguzismailuysal@gmail.com; helo=mail-qv1-xf2c.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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: <20200828152846.GI931@eeg.ccf.org> Xref: csiph.com gnu.bash.bug:16830 28 A=C4=9Fustos 2020 Cuma tarihinde Greg Wooledge yaz= d=C4=B1: > On Fri, Aug 28, 2020 at 10:56:34AM +0200, Binarus wrote: > > #!/bin/bash > > > > function Dummy() { > > > > local -n namerefArray=3D"$1" > > local -a -i myArray=3D("${namerefArray[@]}") > > > > local -p > > } > > > > declare -a -i myArray=3D('1' '2' '3') > > You've got a local variable with the same name as the global variable > that you're attempting to pass by reference. This will not work. > > These scripts yield identical output on bash-5.1 though. > Namerefs (declare -n) in bash are *not* like uplevel commands in Tcl. > They cause the referenced variable name to be evaluated just like any > other variable would be, starting at the current function scope, then > going up to the caller, and so on. > > If you want to use namerefs in a function in bash, you MUST go out of > your way to minimize the chances of a collision between the caller's > variable refererance and ANY local variable of the function. Not just > the nameref itself, but any other incidental variables used in the > function. (As you aptly demonstrated here.) > > So, you can't write functions like this: > > func1() { > declare -n ref=3D"$1" > local i > ... > } > > Instead, you need crazy things like this: > > func1() { > declare -n _func1_ref=3D"$1" > local _func1_i > ... > } > > This doesn't make the slightest sense. What is the point of having local variables then? > And then you just have to pray that the caller respects you enough not > to use variables named with _func1_ prefixes. > > There is no 100% bulletproof solution to this issue. > > See also . > > --=20 O=C4=9Fuz