[Top][All Lists]

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

Cannot shadow local -r when assigning local to array in declaration

From: Ross Goldberg
Subject: Cannot shadow local -r when assigning local to array in declaration
Date: Thu, 23 Apr 2020 20:02:18 -0400

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: darwin19.3.0
Compiler: clang
Compilation CFLAGS: -DSSH_SOURCE_BASHRC -Wno-parentheses
uname output: Darwin Thrawn.local 19.4.0 Darwin Kernel Version 19.4.0: Wed
Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64
Machine Type: x86_64-apple-darwin19.3.0

Bash Version: 5.0
Patch Level: 16
Release Status: release

If a function, f, declares a local readonly variable, v, using local -r, an
error occurs if f calls another function, g, that declares its own local
variable v (therefore shadowing v from f) and that assigns v to an array in
the same statement as the shadowing declaration.

g can, however, assign v to a scalar in a shadowing declaration.

g can also assign a shadowing v to an array, as long as it isn't assigned
in the same statement as the declaration.


function aaa() {
        local x='1 2 3'
        echo "aaa ${x}"

function bbb() {
        local x
        x=(4 5 6)
        echo "bbb ${x[*]}"

function ccc() {
        local x=(7 8 9)
        echo "ccc ${x[*]}"

function ddd() {
        local -r x='0'
        echo "ddd ${x}"


# the above outputs the following:

ddd 0
aaa 1 2 3
bbb 4 5 6
bash: x: readonly variable

reply via email to

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