cod*_*ter 2 bash shell subshell
在我的 Bash 会话中,我运行以下命令:
(echo $$ $BASHPID $-)
Run Code Online (Sandbox Code Playgroud)
我得到
22108 25602 himBH
Run Code Online (Sandbox Code Playgroud)
因此,我的子 shell 作为交互式 shell 运行。
如果我尝试在后台运行相同的命令
(echo $$ $BASHPID $-) &
Run Code Online (Sandbox Code Playgroud)
我得到相同的输出。为什么子shell作为交互式shell运行?
子 shell继承了父级的所有特征,包括设置。这是子外壳定义的一部分。它是在前台还是后台运行没有区别 - 可以很容易地在前台和后台之间交换作业。
非子shell 不会是交互式的。例如,如果我把
(echo $$ $BASHPID $-)
Run Code Online (Sandbox Code Playgroud)
进入一个被gash.sh调用的脚本并从非交互式 shell 调用它:
$ bash gash.sh
73879 73880 hB
Run Code Online (Sandbox Code Playgroud)
但是如果我获取文件,那么它是交互式 shell 的子 shell:
$ . ./gash.sh
1130 himBH
Run Code Online (Sandbox Code Playgroud)