How do I create both local
and declare -r
(read-only) variable in bash?
If I do:
function x {
declare -r var=val
}
Then I simply get a global var
that is read-only
If I do:
function x {
local var=val
}
If I do:
function x {
local var=val
declare -r var
}
Then I get a global again (I can access var
from other functions).
How to combine both local and read-only in bash?
declare
is documented as declaring a local variable, unless you explicitly request a global one by setting the-g
flag. I've just tested on my own system, and it behaves as documented. Can you clarify why you believe this is not the case?