为何无论是否启用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
相关产品推荐
相关产品推荐

