shell 运行 命令是否在子 shell 中的管道之前?

Does the shell run commands before pipe in a subshell?

x=0; { x=1; echo $x; } | cat; echo $x

打印

1
0

如我所料

1
1.

为什么?最后,大括号不创建子外壳。

我用 bash 和 busybox sh (ash) 进行了测试。

In bash both 管道的两侧在 subshells (https://www.gnu.org/software/bash/manual/bashref.html#Pipelines) 中是 运行 除非你 shopt -s lastpipe; set +m,其中管道中的last命令在当前shell

中执行
$ sum=0; seq 10 | while read n; do ((sum+=n)); done; echo $sum
0
$ shopt -s lastpipe
$ sum=0; seq 10 | while read n; do ((sum+=n)); done; echo $sum
0
$ set +m
$ sum=0; seq 10 | while read n; do ((sum+=n)); done; echo $sum
55

在您的示例中,管道中的第一个命令将始终在子shell中运行。

我不能代表ash