[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
Re: curiosity: 'typeset -xr' vs. 'export -r', Robert Elz, 2022/12/12
Re: curiosity: 'typeset -xr' vs. 'export -r', Chet Ramey, 2022/12/12