help-bash
[Top][All Lists]
Advanced

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

Re: local -r for arrays


From: Chet Ramey
Subject: Re: local -r for arrays
Date: Mon, 26 Oct 2020 11:43:32 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.3.3

On 10/25/20 2:42 PM, Marco Ippolito wrote:
> I can do this:
> 
> g1() {
>     local -r a=2$1
>     echo "$a"
> }
> 
> f1() {
>     local -r a=1
>     g1 "$a"
> }
> 
> f1 -> "21"
> 
> 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"
> 
> How does this "asymmetry" come about?

It's an order-of-evaluation problem with certain classes of local variables
`shadowing' variables at a previous context that is fixed in bash-5.1.

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