一种。 { echo "Hello World"; } >outputfile
B. ( echo "Hello World" ) >outputfile
C。 ./anothershell.sh
D. /bin/echo "Hello World"
哪个是对的?以及什么样的命令可以在当前shell的同一个进程中运行?
Cel*_*ada 14
只有 A 会在当前 shell 的进程中运行。
B 将在子 shell 中运行,因为您通过使用 paranehteses 请求子 shell。
C 和 D 都将在当前 shell 进程之外运行,因为它们是对外部命令的调用。
dmc*_*ten 10
您已经多次询问如何证明这一点。您使用环境变量设置作为探针:
export testvar=nope
{ echo "Hello World"; export testvar=yep; } >outputfile
printenv testvar
Run Code Online (Sandbox Code Playgroud)
写 yep
export testvar=nope
( echo "Hello World"; export testvar=yep; ) >outputfile
printenv testvar
Run Code Online (Sandbox Code Playgroud)
写 nope
您需要在 (C) 中编辑脚本并为 (D) 编译修改后的 echo,但随后它们的相应构造也会编写nope
. 这清楚地表明,只有评论中的案例 (A) 和glennjackman 的案例 (E)在同一进程中运行。