bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash silently exits where attempting to assign an array to certain b


From: Kerin Millar
Subject: Re: Bash silently exits where attempting to assign an array to certain built-in variables using declare
Date: Thu, 29 Jun 2023 14:38:05 +0100

On Thu, 29 Jun 2023 08:51:58 -0400
Chet Ramey <chet.ramey@case.edu> wrote:

> On 6/28/23 1:14 PM, Kerin Millar wrote:
> > This report is based on an observation made within the depths of this 
> > thread: https://lists.gnu.org/archive/html/bug-bash/2023-06/msg00094.html.
> > 
> > Attempting to assign an array to any of the following variables with the 
> > declare builtin causes bash to immediately exit with no diagnostic message 
> > being issued.
> > 
> >    BASH_ARGC
> >    BASH_ARGV
> >    BASH_LINENO
> >    BASH_SOURCE
> >    GROUPS
> 
> These are all `noassign' variables; assignments to them are ignored.
> The bash debugger variables cannot be unset either. Other noassign
> variables can be unset; that's why they're not readonly.
> 
> (Before you ask, noassign variables have been in bash since 1996.)

Thanks for this information. While I had no expectation of them being 
assignable, some of this might have a valid place in the manual.

> 
> In this case, assignment to the noassign variable is being treated like an
> assignment error, which aborts the current command (a compound command in
> your examples) but does not exit the shell. If you were to separate the
> commands with a newline, you'd see the difference.

Ah. I thought that I had tried it at some point. Evidently, my manner of 
testing was faulty.

> 
> This came up in 2021 in the GROUPS case:
> 
> https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00013.html
> 
> The other variables in that list inherited their noassign property from
> changes to support the bash debugger.
> 
> > It does not happen if trying to assign a string.
> 
> It should be consistent, at least. I think the string assignment behavior
> is the most reasonable: assignments return 1 but there's no assignment
> error. I'll look at how compound assignments are different.

Thanks. I would just add that 'noassign' variables are not consistently 
indicated in the manual. For example, it is said for GROUPS that "Assignments 
to GROUPS have no effect" but there is no such wording for the others that I 
mentioned. That might be worth addressing, even if only by inserting similarly 
worded sentences where appropriate.

> 
> > 
> >    $ bash -c 'declare BASH_ARGC=1; echo FIN'
> >    FIN
> > 
> > There are various other variables bearing the readonly attribute for which 
> > this also happens. In the following case, bash does, at least, complain 
> > that the variable is readonly.
> > 
> >    $ bash -c 'declare BASHOPTS=(); echo FIN'
> >    bash: line 1: BASHOPTS: readonly variable
> 
> Attempted assignment to readonly variables is an assignment error.
> 
> > This seems rather inconsistent. Also, it is confusing for bash to quit 
> > without indicating why it did so.
> 
> Technically, it exited because it hit EOF after aborting the compound
> command.

I see.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]