You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无论是否启用lastpipe,BASH_SUBSHELL的值始终为0?

Bash管道与子shell行为验证

文档说明

在创建多命令管道时,默认情况下每个命令都会在独立的子shell(一个单独进程)中执行。若使用shopt内置命令启用lastpipe选项,且作业控制未激活时,管道的最后一个元素可能由shell进程直接运行,而非子shell。

测试代码

#!/bin/bash

set +m

shopt -u lastpipe
shopt | grep lastpipe

function test() {
  echo "This is first pipe"  | echo $BASH_SUBSHELL
}

test

shopt -s lastpipe
shopt | grep lastpipe

function test() {
  echo "This is first pipe"  | echo $BASH_SUBSHELL
}

test

实际输出

lastpipe        off
0
lastpipe        on
0

预期输出

lastpipe        off
1
lastpipe        on
0

内容的提问来源于stack exchange,提问作者matrixzj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:28:10