linux – bash和ksh之间的子shell差异

我一直认为子壳不是子过程,而是另一个
shell环境在同一个进程中.

我使用一组基本的内置函数:

(echo "Hello";read)

在另一个终端:

ps -t pts/0
  PID TTY          TIME CMD
20104 pts/0    00:00:00 ksh

所以,kornShell(ksh)中没有子进程.

输入bash,看起来行为不同,给出相同的命令:

  PID TTY          TIME CMD
 3458 pts/0    00:00:00 bash
20067 pts/0    00:00:00 bash

所以,bash中的子进程.
从阅读bash的手册页,很明显,为子shell创建了另一个进程,
然而它假装$$,这是偷偷摸摸的.

bash和ksh之间的区别是预期的,还是我错误地阅读了这些症状?

编辑:其他信息:
在bash上运行strace -f和在Linux上运行ksh表明bash为示例命令调用了两次clone(它不调用fork).所以bash可能正在使用线程(我尝试过ltrace但是核心转储了!).
KornShell既不调用fork,vfork,也不调用clone.

ksh93非常难以避免使用子弹.部分原因是避免使用stdio并广泛使用sfio,这允许内置程序直接通信.另一个原因是ksh在理论上可以有这么多内置.如果使用SHOPT_CMDLIB_DIR构建,则默认情况下包含并启用所有cmdlib内置函数.我不能给出一个避免子壳的地方的完整列表,但通常只在使用内置的情况下,并且没有重定向的情况下.

#!/usr/bin/env ksh

# doCompat arr
# "arr" is an indexed array name to be assigned an index corresponding to the detected shell.
# 0 = Bash, 1 = Ksh93, 2 = mksh
function doCompat {
    ${1:+:} return 1
    if [[ ${BASH_VERSION+_} ]]; then
        shopt -s lastpipe extglob
        eval "${1}[0]="
    else
        case "${BASH_VERSINFO[*]-${!KSH_VERSION}}" in
            .sh.version)
                nameref v=$1
                v[1]=
                if builtin pids; then
                    function BASHPID.get { .sh.value=$(pids -f '%(pid)d'); }
                elif [[ -r /proc/self/stat ]]; then
                    function BASHPID.get { read -r .sh.value _ </proc/self/stat; }
                else
                    function BASHPID.get { .sh.value=$(exec sh -c 'echo $PPID'); }
                fi 2>/dev/null
                ;;
            KSH_VERSION)
                nameref "_${1}=$1"
                eval "_${1}[2]="
                ;&
            *)
                if [[ ! ${BASHPID+_} ]]; then
                    echo 'BASHPID requires Bash, ksh93, or mksh >= R41' >&2
                    return 1
                fi
        esac
    fi
}

function main {
    typeset -a myShell
    doCompat myShell || exit 1 # stripped-down compat function.
    typeset x

    print -v .sh.version
    x=$(print -nv BASHPID; print -nr " $$"); print -r "$x" # comsubs are free for builtins with no redirections 
    _=$({ print -nv BASHPID; print -r " $$"; } >&2)        # but not with a redirect
    _=$({ printf '%s ' "$BASHPID" $$; } >&2); echo         # nor for expansions with a redirect
    _=$(printf '%s ' "$BASHPID" $$>&2); echo # but if expansions aren't redirected, they occur in the same process.
    _=${ { print -nv BASHPID; print -r " $$"; } >&2; }     # However, ${ ;} is always subshell-free (obviously).
    ( printf '%s ' "$BASHPID" $$); echo                   # Basically the same rules apply to ( )
    read -r x _ <<<$(</proc/self/stat); print -r "$x $$"   # These are free in {{m,}k,z}sh. Only Bash forks for this.
    printf '%s ' "$BASHPID" $$| cat # Sadly, pipes always fork. It isn't possible to precisely mimic "printf -v".
    echo
} 2>&1

main "$@"

出:

Version AJM 93v- 2013-02-22
31732 31732
31735 31732
31736 31732 
31732 31732 
31732 31732
31732 31732 
31732 31732
31738 31732

所有这些内部I / O处理的另一个巧妙结果是一些缓冲问题就会消失.这是一个有趣的例子,用tee和head builtins读取线条(不要在任何其他shell中尝试这个).

 $ksh -s <<\EOF
integer -a x
builtin head tee
printf %s\\n {1..10} |
    while head -n 1 | [[ ${ { x+=("$(tee /dev/fd/{3,4})"); } 3>&1; } ]] 4>&1; do
        print -r -- "${x[@]}"
    done
EOF
1
0 1
2
0 1 2
3
0 1 2 3
4
0 1 2 3 4
5
0 1 2 3 4 5
6
0 1 2 3 4 5 6
7
0 1 2 3 4 5 6 7
8
0 1 2 3 4 5 6 7 8
9
0 1 2 3 4 5 6 7 8 9
10
0 1 2 3 4 5 6 7 8 9 10
翻译自:https://stackoverflow.com/questions/14686872/sub-shell-differences-between-bash-and-ksh

转载注明原文:linux – bash和ksh之间的子shell差异