Left side of pipe is the subshell?

前端 未结 2 1399
灰色年华
灰色年华 2020-12-03 03:35

Edit:

My comment below regarding sed \'s@^@ @\' <(f1) is incorrect While $BASH_SUBSHELL indicates that we are in the same level as

相关标签:
2条回答
  • 2020-12-03 04:22

    Here is a much concise example if someone cares :

    cd / && cd /tmp/ | pwd  ; pwd
    /
    /
    

    Or :

    cd / && cd /tmp/ | cd /var/  ; pwd
    /
    

    Yes this page says it all

    http://linux.die.net/man/1/bash# Each command in a pipeline is executed as a separate process (i.e., in a subshell).

    0 讨论(0)
  • 2020-12-03 04:34

    From the bash man page: "Each command in a pipeline is executed as a separate process (i.e., in a subshell)." I suppose it would be possible to execute one component of a pipeline in the current shell (i.e. the first, or the last, or maybe one in the middle), it doesn't play favorites like this: they all execute in subshells. If you modify your script like this:

    #!/bin/bash
    declare -i i=0
    function f1()
    {
        let i++
        echo "In f1, SUBSHELL: $BASH_SUBSHELL, i=$i" >&2
    }
    
    f1
    f1 | f1 | f1
    
    echo "at end, i=$i"
    

    it prints:

    In f1, SUBSHELL: 0, i=1
    In f1, SUBSHELL: 1, i=2
    In f1, SUBSHELL: 1, i=2
    In f1, SUBSHELL: 1, i=2
    at end, i=1
    

    because all 3 invocations of f1 in the pipeline run in subshells.

    0 讨论(0)
提交回复
热议问题