Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: var=$( typeset "$1" ) ... not within a function.



On Thu, Oct 20, 2022 at 1:48 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> typeset scares
> me because sometimes it's a passive reporter, other times dangerously
> active

Yes, it's unfortunate that typeset (without options) was ever given
the function you describe as "passive reporter".  Worse that it's
synonym "declare" behaves the same.

> in this case I was just looking for information

There are two typeset options specifically for that purpose: -p, which
prints "typeset" commands so you can replay them e.g. with "eval" or
from a script file, and "+" (all by itself), which either prints all
the parameter names (and only the names), or the assignment form of
whatever names follow it.  (The manual still doesn't explain the
latter effect, I see, claiming that nothing is allowed to follow a
bare "+" sign.)

> but it seems
> like I created a parameter -- which '-m' seems to prevent.

The -m option actually is for pattern-matching and is intended to be
combined with other options ... when you use it without any others, it
has the effect of canceling "what typeset does when there are no
options", and that accidentally produces the effect you wanted.
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 641e46cf9..dd54a0fc8 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -2047,8 +2047,9 @@ startitem()
 item(tt(PLUS()))(
 If `tt(PLUS())' appears by itself in a separate word as the last option,
 then the names of all parameters (functions with tt(-f)) are printed, but
-the values (function bodies) are not.  No var(name) arguments may appear,
-and it is an error for any other options to follow `tt(PLUS())'.  The
+the values (function bodies) are not.  If var(name) arguments appear,
+both those names and their values are printed in the form of assignments.
+It is an error for any other options to follow `tt(PLUS())', but the
 effect of `tt(PLUS())' is as if all attribute flags which precede it were
 given with a `tt(PLUS())' prefix.  For example, `tt(typeset -U PLUS())' is
 equivalent to `tt(typeset +U)' and displays the names of all arrays having
@@ -2081,7 +2082,8 @@ Except when assignments are made with var(name)tt(=)var(value), using
 tt(+m) forces the matching parameters and their attributes to be printed,
 even inside a function.  Note that tt(-m) is ignored if no patterns are
 given, so `tt(typeset -m)' displays attributes but `tt(typeset -a +m)'
-does not.
+does not.  Ordinary scalar string parameters have no attributes, so for
+those tt(+m) prints only the names.
 )
 item(tt(-p) [ var(n) ])(
 If the tt(-p) option is given, parameters and values are printed in the


Messages sorted by: Reverse Date, Date, Thread, Author