使用 /bin/zsh -c print 时如何显示输出?

How to show output when using /bin/zsh -c print?

我在使用 /bin/zsh -cprint 命令时感到困惑,没有输出。

如何在 /bin/zsh -c 的下一个提示中得到 foo

$ print -z foo
$ foo
(↑this is expected result that foo is in next prompt)

$ /bin/zsh -c print -z foo

$
(not show foo in next prompt)

运行 它在当前 shell,而不是 child。

$ print -z echo hello
$ echo hello             <-- Already in the buffer, only pressing ENTER now 
hello
$

发生这种情况是因为 -z 选项打印到编辑缓冲区(用于下一个提示)。所以如果你 运行:

$ /bin/zsh -c 'print -z FOO'

它将在那个/bin/zsh -c子shell的下一个提示编辑缓冲区中推送FOO。但是 sub-shell 将在 运行ning print -z FOO 之后立即退出,因此 FOO 最终将被丢弃。 subshell 无法操纵其父 shell 编辑缓冲区(它们是完全不同的进程,具有自己的编辑缓冲区)。

如果你只想将 FOO 注入当前 shell 的下一个命令行,只需 运行:

$ builtin print -z FOO

下一个提示将如下所示(_ 光标等待用户输入):

$ FOO_