Sub-shell differences between bash and ksh

后端 未结 3 1977
时光说笑
时光说笑 2020-12-06 12:33

I always believed that a sub-shell was not a child process, but another shell environment in the same process.

I use a basic set of built-ins:

(echo          


        
3条回答
  •  醉酒成梦
    2020-12-06 13:08

    In ksh, a subshell might or might not result in a new process. I don't know what the conditions are, but the shell was optimized for performance on systems where fork() was more expensive than it typically is on Linux, so it avoids creating a new process whenever it can. The specification says a "new environment", but that environmental separation may be done in-process.

    Another vaguely-related difference is the use of new processes for pipes. In ksh and zsh, if the last command in a pipeline is a builtin, it runs in the current shell process, so this works:

    $ unset x
    $ echo foo | read x
    $ echo $x
    foo
    $
    

    In bash, all pipeline commands after the first are run in subshells, so the above doesn't work:

    $ unset x
    $ echo foo | read x
    $ echo $x
    
    $
    

    As @dave-thompson-085 points out, you can get the ksh/zsh behavior in bash versions 4.2 and newer if you turn off job control (set +o monitor) and turn on the lastpipe option (shopt -s lastpipe). But my usual solution is to use process substitution instead:

    $ unset x
    $ read x < <(echo foo)
    $ echo $x
    foo
    

提交回复
热议问题