On Wed, Apr 3, 2013 at 11:21 AM, Pierre Gaston <pierre.gas...@gmail.com>wrote:
> On Wed, Apr 3, 2013 at 11:03 AM, Chris Down <ch...@chrisdown.name> wrote: > >> On 2013-04-03 11:00, Nikolai Kondrashov wrote: >> > >>>It doesn't work because you are trying to redefine an existing >> > >>>readonly variable. >> > >> >> > >>Yes, but I'm explicitly redefining it locally, only for this function. >> > >>And this works for variables previously defined in the calling >> function. >> > > >> > >You're not redefining it locally, you are unsuccessfully trying to >> override a >> > >global. >> > >> > Still Nikolai has a point. > It's not clear why readonly variable can be overridden when the variable > is declared readonly in the scope of an englobing function but not if it is > declared readonly in the global scope. > > $ bash -c 'a() { v=2;echo "$v"; }; b () { declare -r v=1; a; echo "$v"; > }; b' > bash: v: readonly variable > > The variable is locale to b, but the readonly flag is preserved in a > > $ bash -c 'a() { declare -r v=2;echo "$v"; }; b () { declare -r v=1; a; > echo "$v"; }; b' > 2 > 1 > > The variable is locale to b, but you can redeclare it locale to a even if > it has the readonly flag > > $ bash -c 'declare -r v=2; b () { declare -r v=1; echo "$v"; }; b' > bash: line 0: declare: v: readonly variable > 2 > > it looks like the same as the first case except that the variable is > declared readonly in the global scope. > > (Also readonly defers from declare -r: > bash -c 'a() { declare -r v=2;echo "$v"; }; b () { readonly v=1; a; echo > "$v"; }; b; v=2' > $ bash -c 'a() { declare -r v=2;echo "$v"; }; b () { readonly v=1; a; echo > "$v"; }; b; v=2' > bash: line 0: declare: v: readonly variable > 1 > 1 > bash: v: readonly variable. > > I seem to recall this has been discussed on this list at some point) > ok it has indeed been discussed, and this is a feature not a bug: http://lists.gnu.org/archive/html/bug-bash/2012-04/msg00088.html