[Toybox] So why does local support += then?

Chet Ramey chet.ramey at case.edu
Mon Aug 24 18:41:55 UTC 2026


On 8/24/26 1:43 PM, Rob Landley wrote:
> On 8/23/26 15:24, Chet Ramey wrote:
>> On 8/22/26 4:58 PM, Rob Landley wrote:
>>> func() { local PP+=abc; echo $PP; }; PP=123; func; echo $PP
>>>
>>> I went and implemented "123abc\n123\n" as the output,
>>
>> Do your local variables inherit a value from an enclosing scope in other
>> contexts?
> 
> local PP=$PP

Sure, but we're not doing that here, are we? That's not inheriting a value,
that's expanding the rhs of the assignment statement before the local
variable is created.

> It's that local is appending _while_creating_. It was told to append in the 
> identification of the variable to make local.

It doesn't matter. In this simple case, local variables do not inherit any
value to append to by default.

> It's not a question of "want". I'm just trying to understand the reasoning.

OK. Local variables do not inherit values from a variable with the same
name at an enclosing scope unless you force this behavior.

Stuff like `x=4 local x' has to be handled, stuff like `x=env func', where
`func' declares x as local, has to be handled, since those are supposed to
be in the same scope as the call to local.
>> If you want an error message, use `unset -v', so unset knows it's only
>> looking for variable names. That's why it exists.
> 
> Ah, I need to add another special case error message and a test.

Well, you need to check whether the argument(s) to `unset -v' are valid
shell identifiers. Bash complains about this; some other shells do not.
You already check for that in other places.
> The question that got me there wasn't "does it accept =" but "is this 
> another place where = can instead be +=" (the alias plumbing doesn't take 
> += for example).

declare/typeset/local, export, and readonly are the declaration commands.
(The first three are more or less equivalent.)

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet at case.edu    http://tiswww.cwru.edu/~chet/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: OpenPGP digital signature
URL: <http://lists.landley.net/pipermail/toybox/attachments/20260824/0e5e6c5d/attachment.sig>


More information about the Toybox mailing list