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 c...@case.edu http://tiswww.cwru.edu/~chet/