[Top][All Lists]

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

Re: curiosity: 'typeset -xr' vs. 'export -r'

From: Lawrence Velázquez
Subject: Re: curiosity: 'typeset -xr' vs. 'export -r'
Date: Tue, 13 Dec 2022 03:07:16 -0500
User-agent: Cyrus-JMAP/3.7.0-alpha0-1115-g8b801eadce-fm-20221102.001-g8b801ead

On Mon, Dec 12, 2022, at 4:43 AM, L A Walsh wrote:
> On 2022/12/11 20:47, Lawrence Velázquez wrote:
>> This happens because "declare"/"typeset" creates local variables
>> within functions.  Using -g works around this...
>>      $ Export() { declare -gx "$@"; }
>>      $ Export -r foo=1
>>      $ declare -p foo
>>      declare -rx foo="1"
>> ...but now "Export" always creates global variables, rather than
>> scoping as "declare" and your alias-based version does.  On the
>> other hand, "export" also creates global variables, so in a sense
>> the workaround version is more consistent.
>>      $ f() { export "$@"; }
>>      $ f var=1
>>      $ declare -p var
>>      declare -x var="1"
> ----
> I see, but you still can't use "-r" w/export

Of course not.  I only meant to demonstrate that "export" always
creates global variables, so a function that utilizes "declare -gx"
actually behaves more like "export" then your alias does.

> though I think the -r flag would get dropped in any exported shell

The environment does not propagate the readonly attribute, no.

> though
> in that case, one might expect an error if one uses "typeset -xr"
> along the lines of "-r flag won't be exported".

The readonly attribute is still applied to the original shell
variable, so there's no reason to produce an error.

> Side curious: If one uses -g, does it cause the var to be defined
> in all intervening functions as well as the top(global)
> and current scope?

It defines a global variable, which is then visible to all functions.


reply via email to

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