bug-bash
[Top][All Lists]
Advanced

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

Re: Assignment to RO variable


From: Chet Ramey
Subject: Re: Assignment to RO variable
Date: Wed, 16 Aug 2023 09:29:26 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.14.0

On 8/15/23 9:57 PM, Wiley Young wrote:

 From within a stack of x3 functions called from a case within a for loop,
when trying to assign a value to a read-only variable, `printf` and `read`
both fail with exit codes of 1, and the functions, `case` and `for`
commands all complete.  When trying effectively the same thing with a
simple assignment syntax, `x=z`, when the assignment fails, the functions
return, `case` fails to complete and `for` returns an exit code of 1.

Assigning to a readonly variable using an assignment statement is a
variable assignment error, which causes non-interactive shells to exit
(POSIX mode) or return to the top level to read another command (default
mode). Interactive shells abandon execution of the command and return
to the top level.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01 says

"If any of the variable assignments attempt to assign a value to a variable for which the readonly attribute is set in the current shell environment (regardless of whether the assignment is made in that environment), a variable assignment error shall occur. See Consequences of Shell Errors for the consequences of these errors."

Builtins that assign variables as a side effect of execution (e.g., read,
printf, getopts) do not treat attempts to assign to readonly variables as
variable assignment errors; they simply cause the builtin to fail and
return a non-zero status.

--
``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/




reply via email to

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