* [PATCH] More stuff for Etc/FAQ
@ 2024-01-13 20:48 Bart Schaefer
2024-01-14 20:46 ` Lawrence Velázquez
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2024-01-13 20:48 UTC (permalink / raw)
To: Zsh hackers list
[-- Attachment #1: Type: text/plain, Size: 88 bytes --]
The file-slurping thread on zsh-users brought me here.
Anything else worth mentioning?
[-- Attachment #2: faq-nofork-slurp.txt --]
[-- Type: text/plain, Size: 2057 bytes --]
diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
index 270a04608..98bc5dce4 100644
--- a/Etc/FAQ.yo
+++ b/Etc/FAQ.yo
@@ -1067,6 +1067,41 @@ label(211)
quoted, that is, included in a quoted string or prefixed by backslash.
These substitutions first become usable after zsh 5.9.
+sect(Comparisons of forking and non-forking command substitution)
+
+ mytt(${ command }) and variants may change the caller's options by using
+ mytt(setopt) and may modify the caller's local parameters, including the
+ positional parameters mytt($1), mytt($2), etc., via both assignments and
+ mytt(set -- pos1 pos2 etc). Nothing that happens within mytt($(command))
+ affects the caller.
+
+ mytt($(command)) removes trailing newlines from the output of mytt(command)
+ when substituting, whereas mytt(${ command }) and its variants do not.
+
+ When mytt(command) is myem(not) a builtin, mytt(${ command }) does fork, and
+ typically forks the same number of times as mytt($(command)), because in
+ the latter case zsh usually optimizes the final fork into an exec.
+
+ Redirecting input from files has subtle differences:
+
+ mytt($(<file)) is optimized to read from mytt(file) without forking, but
+ per above it removes trailing newlines.
+
+ mytt(${<file}) is a substitution error.
+
+ mytt(${ <file }) copies mytt(file) using the mytt(NULLCMD) programs, then
+ reads and substitutes the contents of the copy. Also, this fails if the
+ mytt(CSH_NULLCMD) or mytt(SH_NULLCMD) options are in effect, so it does
+ not work in emulation modes.
+
+ mytt(${|<file}) copies mytt(file) to the standard output using mytt(NULLCMD)
+ but substitutes nothing because there is no assignment to mytt(REPLY). It
+ fails in emulation modes.
+
+ mytt(${|IFS= read -rd '' <file}) is therefore the best solution for files
+ that do not contain nul bytes, because it copies the file directly into
+ the local mytt(REPLY) and then substitutes that.
+
chapter(How to get various things to work)
sect(Why does mytt($var) where mytt(var="foo bar") not do what I expect?)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] More stuff for Etc/FAQ
2024-01-13 20:48 [PATCH] More stuff for Etc/FAQ Bart Schaefer
@ 2024-01-14 20:46 ` Lawrence Velázquez
2024-01-14 22:37 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Lawrence Velázquez @ 2024-01-14 20:46 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-workers
On Sat, Jan 13, 2024, at 3:48 PM, Bart Schaefer wrote:
> The file-slurping thread on zsh-users brought me here.
>
> Anything else worth mentioning?
>
> [...]
>
> + mytt($(command)) removes trailing newlines from the output of mytt(command)
> + when substituting, whereas mytt(${ command }) and its variants do not.
Hm, this diverges from the behavior of (m)ksh and (soon) bash:
% cat ./nofork_test; echo
echo "$KSH_VERSION$BASH_VERSION$ZSH_PATCHLEVEL"
foo=${ echo abc; }
typeset -p foo
% ksh ./nofork_test; echo
Version AJM 93u+ 2012-08-01
foo=abc
% mksh ./nofork_test; echo
@(#)MIRBSD KSH R59 2020/10/31
typeset foo=abc
% Reference/bash/bash ./nofork_test; echo
5.3.0(2)-devel
declare -- foo="abc"
% zsh/zsh/Src/zsh ./nofork_test
zsh-5.9-342-gdde1259
typeset foo=$'abc\n'
If this is intentional, then it might be worth mentioning in the
FAQ, to warn users who are used to the other behavior.
--
vq
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] More stuff for Etc/FAQ
2024-01-14 20:46 ` Lawrence Velázquez
@ 2024-01-14 22:37 ` Bart Schaefer
2024-01-15 0:39 ` Lawrence Velázquez
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2024-01-14 22:37 UTC (permalink / raw)
To: Lawrence Velázquez; +Cc: zsh-workers
On Sun, Jan 14, 2024 at 12:47 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> > + mytt($(command)) removes trailing newlines from the output of mytt(command)
> > + when substituting, whereas mytt(${ command }) and its variants do not.
>
> Hm, this diverges from the behavior of (m)ksh and (soon) bash:
This was based on the discussion (and Sebastian's sample code) of how
mksh ${|REPLY=$'abc\n'} works. I thought it better to be consistent
for all of these forms.
I'm disinclined to change it for native zsh, but I will look at
whether it can easily be changed either based on emulation mode or a
setopt (SH_WORD_SPLIT maybe? Hrm).
Defintely worth mentioning in the FAQ tho.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] More stuff for Etc/FAQ
2024-01-14 22:37 ` Bart Schaefer
@ 2024-01-15 0:39 ` Lawrence Velázquez
0 siblings, 0 replies; 4+ messages in thread
From: Lawrence Velázquez @ 2024-01-15 0:39 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-workers
On Sun, Jan 14, 2024, at 5:37 PM, Bart Schaefer wrote:
> On Sun, Jan 14, 2024 at 12:47 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>>
>> > + mytt($(command)) removes trailing newlines from the output of mytt(command)
>> > + when substituting, whereas mytt(${ command }) and its variants do not.
>>
>> Hm, this diverges from the behavior of (m)ksh and (soon) bash:
>
> This was based on the discussion (and Sebastian's sample code) of how
> mksh ${|REPLY=$'abc\n'} works. I thought it better to be consistent
> for all of these forms.
I'm inclined to agree, despite the inconsistency with $(...) and
the other shells. In zsh it will be easy to use something like
foo=${${ cmd }%$'\n'}
if desired, while in the other shells preserving trailing LFs is
still annoying.
> Defintely worth mentioning in the FAQ tho.
A quick aside in the manual might also be good.
--
vq
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-01-15 0:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-13 20:48 [PATCH] More stuff for Etc/FAQ Bart Schaefer
2024-01-14 20:46 ` Lawrence Velázquez
2024-01-14 22:37 ` Bart Schaefer
2024-01-15 0:39 ` Lawrence Velázquez
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).