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

2022-12-12 Thread Chet Ramey

On 12/11/22 9:37 PM, L A Walsh wrote:

  This is mostly a 'nit', but I noticed I had
    "typeset -xr"
  in one of my scripts to mean export+read-only and
  was wondering why
    "export -r"
  was disallowed (err message):

bash: export: -r: invalid option
export: usage: export [-fn] [name[=value] ...] or export -p

This seems to be an unnecessary "make-wrong", no? 


No. There's no compelling reason to provide it. You can already do this,
as you discovered, using a builtin whose purpose is to assign attributes
to variables.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




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

2022-12-12 Thread L A Walsh




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).