help-bash
[Top][All Lists]
Advanced

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

Re: local -r for arrays


From: Marco Ippolito
Subject: Re: local -r for arrays
Date: Mon, 26 Oct 2020 14:42:37 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0

On 25/10/2020 21:32, Lawrence Velázquez wrote:
On Oct 25, 2020, at 2:42 PM, Marco Ippolito <maroloccio@gmail.com> wrote:

but I can't do this:

g2() {
    local -r a=(2 "$@")
    echo "${a[@]}"
}

f2() {
    local -r a=(1)
    g2 "${a[@]}"
}

f2 -> error "a: readonly variable"

Note that your test case works if g2() uses local -a.

g2() {
    local -ar a=(2 "$@")
    echo "${a[@]}"
}

--
vq

True, but even if the "works":

set -x

# declare -ar a=(3)

g2() {
    local -ar a=(2 "$@")
    echo "${a[@]}"
}

f2() {
    local -ar a=(1)
    g2 "${a[@]}"
}

f2 ->
+ f2
+ a=(1)
+ local -ar a
+ g2 1
+ a=(2 "$@")
+ local -ar a
+ echo 2 1
2 1

uncommenting the outer array declaration makes it fail again:

f2 -> a: readonly variable

It is as if we were "borrowing" an outer array in the two-step
approach highlighted by João, before we set "-ar" on a local
with the same name.



reply via email to

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