[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: |
Chet Ramey |
Subject: |
Re: Bash silently exits where attempting to assign an array to certain built-in variables using declare |
Date: |
Thu, 29 Jun 2023 08:51:58 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 |
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.)
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.
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.
$ 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.
Chet
--
``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/