[Top][All Lists]

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

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

From: L A Walsh
Subject: Re: curiosity: 'typeset -xr' vs. 'export -r'
Date: Mon, 12 Dec 2022 01:43:09 -0800
User-agent: Thunderbird

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, though I think
the -r flag would get dropped in any exported shell, though
in that case, one might expect an error if one uses "typeset -xr"
along the lines of "-r flag won't be exported".

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?

NOTE: The original question was about allowing "-r" with export:

        $ f() { export "$@"; }
        $ f -r var=1
export: -r: invalid option
export: usage: export [-fn] [name[=value] ...] or export -p

Seems like it would get rid of an unnecessary error message
and maybe, an inconsistency with "typeset -xr".
Thanks for the info on using -g in the function.  I haven't
used -g too much since one of my machines still used bash-v3 and
I don't think -g appeared until 4.x (don't quote me on that though).

reply via email to

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