Skip to main content
Remove inaccurate data, expand example showing how to test the fact of a subshell. This is the only answer that gives a *demonstration* of this rather than only referring to the docs, so it's well worth keeping.
Source Link
Wildcard
  • 37.5k
  • 30
  • 149
  • 284

I want to use them just for grouping, but is it actually spawning a subshell? (How can I tell?)

It's not spawning a subshell. TheYes, the commands within the (...) just groups the commands and runs themare run in the existing shella subshell.

EDIT: ToTo test whether you're in a subshell you can compare the PID of your current shell with the PID of the interactive shell.

echo $BASHPID; (echo $BASHPID); 

It turns out I was wrong. The commands within the (...) are run inExample output, demonstrating that parentheses spawn a subshell. and curly braces do not:

$ echo $BASHPID; (echo $BASHPID); { echo $BASHPID;}
50827
50843
50827
$ 

It's not spawning a subshell. The (...) just groups the commands and runs them in the existing shell.

EDIT: To test whether you're in a subshell you can compare the PID of your current shell with the PID of the interactive shell.

echo $BASHPID; (echo $BASHPID); 

It turns out I was wrong. The commands within the (...) are run in a subshell.

I want to use them just for grouping, but is it actually spawning a subshell? (How can I tell?)

Yes, the commands within the (...) are run in a subshell.

To test whether you're in a subshell you can compare the PID of your current shell with the PID of the interactive shell.

echo $BASHPID; (echo $BASHPID); 

Example output, demonstrating that parentheses spawn a subshell and curly braces do not:

$ echo $BASHPID; (echo $BASHPID); { echo $BASHPID;}
50827
50843
50827
$ 
added 257 characters in body
Source Link
David King
  • 3.2k
  • 12
  • 25

It's not spawning a subshell. The (...) just groups the commands and runs them in the existing shell.

EDIT: To test whether you're in a subshell you can compare the PID of your current shell with the PID of the interactive shell.

echo $BASHPID; (echo $BASHPID); 

It turns out I was wrong. The commands within the (...) are run in a subshell.

It's not spawning a subshell. The (...) just groups the commands and runs them in the existing shell.

It's not spawning a subshell. The (...) just groups the commands and runs them in the existing shell.

EDIT: To test whether you're in a subshell you can compare the PID of your current shell with the PID of the interactive shell.

echo $BASHPID; (echo $BASHPID); 

It turns out I was wrong. The commands within the (...) are run in a subshell.

Source Link
David King
  • 3.2k
  • 12
  • 25

It's not spawning a subshell. The (...) just groups the commands and runs them in the existing shell.