In my overlay I have an eclass, which makes certain self-declared global variables readonly. This works: ``` VAR="value" readonly VAR ``` but this: ``` declare -r VAR="value" ``` does not, because in phase functions the variable is not defined. From BASH's point of view those two declarations should be the same, so there must be something in Portage internals that makes the variable disappear.
(In reply to Jan Chren (rindeal) from comment #0) > From BASH's point of view those two declarations should be the same, so > there must be something in Portage internals that makes the variable > disappear. Did you actually confirm the bash behavior, or are just guessing? Because if you either tested or read the documentation, you would know that (in some versions of bash, since bash upstream doesn't care to keep the behavior stable) 'declare' defaults to setting variables local rather than global. FOO=11 -- global variable declare FOO=11 -- local variable You are looking for 'declare -g'. I think it's available since bash-4.
I know that(In reply to Michał Górny from comment #1) > (In reply to Jan Chren (rindeal) from comment #0) > > From BASH's point of view those two declarations should be the same, so > > there must be something in Portage internals that makes the variable > > disappear. > > Did you actually confirm the bash behavior, or are just guessing? Confirmed. > Because if > you either tested or read the documentation, you would know that (in some > versions of bash, since bash upstream doesn't care to keep the behavior > stable) 'declare' defaults to setting variables local rather than global. Yes, I know that, but as BASH's global space doesn't have local variables 'declare var' still makes them global, unless global space in an ebuild is not as global as in a pure BASH script, because it's wrapped in some Portage internals. I think that this fact should be mentioned in devmanual with big red letters.
CC-ing PMS people since PMS also uses the term 'global scope' a lot. We may consider avoiding it.