help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Why does `declare` in a function in a subshell interfere wit


From: Peng Yu
Subject: [Help-bash] Why does `declare` in a function in a subshell interfere with a read-only variable in the parent shell?
Date: Sat, 8 Dec 2018 12:46:58 -0600

HI,

The following code shows declare cannot tell whether it is in a
subshell while it is called in a function.

It always yields an error when it sees a read-only variable, even the
read-only variable is from a parent shell.

I am not sure this makes sense. Should this behavior be fixed? Thanks.

$ cat ./x.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
declare x=42
readonly x
function f {
    declare x
}
f
(
    declare x
)
(
    function f {
        declare x
    }
    f
)
12:43:41@/tmp$ ./x.sh
declare x=42
readonly x
function f {
    declare x
}
f
./x.sh: line 8: declare: x: readonly variable
(
    declare x
)
(
    function f {
        declare x
    }
    f
)
./x.sh: line 16: declare: x: readonly variable

-- 
Regards,
Peng



reply via email to

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