Is there a subshell created when I run `sh -c “command”`, a new shell or none of these?

后端 未结 7 741
醉话见心
醉话见心 2020-12-08 23:18

In bash, when I run the following command:

sh -c \"command\"

is there created a subshell and then the command

7条回答
  •  猫巷女王i
    2020-12-08 23:30

    sh will spawn a subshell. But the subshell id stays the same

    21934 pts/0    Ss     0:00 -bash
    21963 pts/0    S      0:00  \_ /usr/bin/sudo -u root -H /bin/bash -c  export AUDITUSER=ch002854; cd $HOME && exec -a '-bash' /bin/bash
    22031 pts/0    S      0:00      \_ -bash
     2969 pts/0    S      0:00          \_ sleep 1000
     2993 pts/0    S      0:00          \_ sleep 1000
     3726 pts/0    R+     0:00          \_ ps af
    

    With sh -c it will just run the command. This will reinitialize your environment variables and therefore it resets $BASH_SUBSHELL to its default 0.

    # sh -c 'echo $BASHPID, $BASH_SUBSHELL'
    12671, 0
    

    While with `` or () you can create a subshell

    # (echo $BASHPID, $BASH_SUBSHELL)
    13214, 1
    

提交回复
热议问题