You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Declaring constant with declare -r or readonly commands seem to remove the ability to overshadow them with local variables. Meaning that once we create a constant variable A and then we reference it in some function that has some local variable inside with the same name A, then we still get an error, because name A is already globally reserved for that particular constant variable created initially.
foo() {
declare -r A=12
}
declare -r A=12
foo # ERROR: A is already declared
The same error occurs for readonly.
To Reproduce
Here is a simple shell script that reproduces the error:
foo() {
readonly test=12
}
# Declaring a constant will cause an error if it's being redeclared in the subsequent linesreadonly test=12
echo"FUNCTIONS"
foo
foo
foo
echo"LOOPS & FUNCTIONS"foriin 0 1 2
do
foo
doneecho"LOOPS"foriin 0 1 2
doreadonly test=12
done
Expected behavior
It should never error in the code above. Each constant is created for each iteration and then goes out of scope. The same fact applies to functions.
I think that the solution to this problem is to replace bash constants with regular variables and just check the constant usage in the compilation process.
The text was updated successfully, but these errors were encountered:
I think that the solution to this problem is to replace bash constants with regular variables and just check the constant usage in the compilation process.
That was my idea :-P Anyway the community voted at the time to do not do it in this way, but if there is no technical solution right now is the best option.
Describe the bug
Declaring constant with
declare -r
orreadonly
commands seem to remove the ability to overshadow them with local variables. Meaning that once we create a constant variableA
and then we reference it in some function that has some local variable inside with the same nameA
, then we still get an error, because nameA
is already globally reserved for that particular constant variable created initially.The same error occurs for
readonly
.To Reproduce
Here is a simple shell script that reproduces the error:
Expected behavior
It should never error in the code above. Each constant is created for each iteration and then goes out of scope. The same fact applies to functions.
I think that the solution to this problem is to replace bash constants with regular variables and just check the constant usage in the compilation process.
The text was updated successfully, but these errors were encountered: