A. { echo "Hello World"; } >outputfile
B. ( echo "Hello World" ) >outputfile
C. ./anothershell.sh
D. /bin/echo "Hello World"
Which is right? And what kind of command can run in the same process of the current shell?
Only A will run within the process of the current shell.
B will run in a subshell because you asked for a subshell by using paranehteses.
C and D will both run outside of the current shell process because they are invocations of external commands.
strace
under Linux or truss
under Solaris, for example. Why? Did you expect a different answer?
You have asked a couple times about how to prove it. You use environment variable setting as a probe:
export testvar=nope
{ echo "Hello World"; export testvar=yep; } >outputfile
printenv testvar
writes yep
export testvar=nope
( echo "Hello World"; export testvar=yep; ) >outputfile
printenv testvar
writes nope
You'll need to edit your script in (C) and compile a modified echo for (D), but then the corresponding constructs for them will also write nope
. This shows explicitly that only case (A) and glennjackman's case (E) from the comments run in the same process.
One distinction you may be missing is that B will fork (so it's another process), but it doesn't actually exec (it's a copy of the same shell, it doesn't go and find /bin/sh and re-run initializers etc). And there are special rules ($$ is the pid of the original shell, for example).
A
andB
echo
ends quickly and difficult to catch its process. ./anothershell.sh
would execute in the current shell.