bug-bash
[Top][All Lists]
Advanced

[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: Sun, 11 Dec 2022 23:47:32 -0500
User-agent: Cyrus-JMAP/3.7.0-alpha0-1115-g8b801eadce-fm-20221102.001-g8b801ead

On Sun, Dec 11, 2022, at 9:37 PM, L A Walsh wrote:
> I suppose one could create an alias (despite advice that
> functions are "better" -- in this case a function doesn't work).
> I'm using ':;' for PS1, so cut/paste works:
>
>   PS1=':; '
>
> :; Export () {
> :;   typeset -x "$@"
> :; }
> :; Export -r foo_xr=1
>
> :; typeset -p foo_xr
> -bash: typeset: foo_xr: not found

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"

-- 
vq



reply via email to

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