bug-bash
[Top][All Lists]
Advanced

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

Re: Local variables overriding global constants


From: Chris Down
Subject: Re: Local variables overriding global constants
Date: Wed, 3 Apr 2013 16:03:41 +0800
User-agent: Mutt/1.5.21 (2010-09-15)

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.
>
> How is this different?
>
>     bash -c 'declare v=1; function a() { declare v=2; }; a; echo "$v"'

From `help declare':

    When used in a function, `declare' makes NAMEs local, as with the `local'
    command.  The `-g' option suppresses this behavior.

> >>This:
> >>
> >>     bash -c 'a() { echo "$v"; }; b() { declare -r v=123; a; }; b'
> >>
> >>Produces this:
> >>
> >>     123
> >
> >That is *inside* the function, not *outside* the function.
>
> I'd say that "v" is declared outside function "a", where it is accessed, or do
> you mean that there is no concept of separate functions in Bash and all
> "functions" are just one function?

$v is not populated at initial runtime, it is populated when it is accessed.

Chris

Attachment: pgpzj2o8e2uMu.pgp
Description: PGP signature


reply via email to

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