bash 4.3 lastpipe bug或功能?

Phi*_*Phi 3 bash pipeline

我尝试在bash中使用lastpipe选项,当从交互式提示命令行运行时,它似乎无法按预期工作.

phi@phiz:~$ shopt lastpipe
lastpipe        on

phi@phiz:~$ echo $BASH_VERSION
4.3.11(1)-release

phi@phiz:~$ echo yo | read a ; echo "a='$a'"
a=''
Run Code Online (Sandbox Code Playgroud)

所以我在$ a中没有得到任何东西,但是如果在子流程中运行它有点工作虽然没用我在顶级交互中需要它

phi@phiz:~$ (echo yo | read a ; echo "a='$a'")
a='yo'
Run Code Online (Sandbox Code Playgroud)

这种行为有望吗?我完成了我的RTFM职责,无法确定这是出于意图还是出乎意料.

我是bash的新手,当来自其他炮弹时,这让人感到困惑

提前感谢任何建议.

小智 6

引用文档:

'lastpipe'
     If set, and job control is not active, the shell runs the last
     command of a pipeline not executed in the background in the
     current shell environment.
Run Code Online (Sandbox Code Playgroud)

在普通的交互式shell中,作业控制处于活动状态,因此该lastpipe选项无效.在子shell中,作业控制不活动,因此该lastpipe选项确实生效.

工作控制指的是能够暂停工作^Z然后再恢复工作的事情fg,因此这是您通常想要的.在你的情况下,我可能会尝试重新写入

a=$(echo yo); echo "a='$a'"
Run Code Online (Sandbox Code Playgroud)

或者,如果你真的只需要其他命令的第一行,请通过它head -n1.

另一方面,如果您确定不需要作业控制,则可以使用set +m(如@chepner所指出的)禁用它,并可选择稍后重新启用它set -m.