On Fri, Jul 15, 2022, at 12:13 PM, Robert Stoll wrote:
> Following a script to reproduce the problem: 
>
> =========================================================================================
>
> #!/usr/bin/env bash
> set -e
>
> function trapHook(){
>       echo "$1"
>       local -r readonlyVar=$1
> }
>
> function test1(){
>       local -r readonlyVar=2
>       trap 'trapHook $readonlyVar' EXIT
> }
> function test2(){
>       local -r readonlyVar=2
>       trap 'trapHook $readonlyVar' EXIT
>       exit 0
> }
> test1 # works as exit happens outside of test1
> test2 # fails with ./src/test.sh: line 6: local: readonlyVar: readonly 
> variable

You can't shadow a readonly variable:

https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00152.html
https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00153.html
https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00201.html
https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00204.html

The trap is incidental -- you can observe the same behavior without
it -- but it's worth noting that the trap command runs in the context
from which the shell exits, not the context in which the trap is
set.

What do you think the bug is, exactly?  How do you think this should
behave?

-- 
vq

Reply via email to