Bash Subshell – All Ways to Create

bashsubshell

There are 2 main ways that I know of so far:

  • Explicitly: wrapping parentheses around a list of commands

  • Implicitly: every command in a pipeline

Are there more ways, either explicitly or implicitly, in which one creates subshells in bash?

Best Answer

From man bash:

  • If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.
  • A coprocess is a shell command preceded by the coproc reserved word.
    A coprocess is executed asynchronously in a subshell, as if the command had been terminated with the & control operator
  • Shell builtin complete command: when called with the -C command option, command is executed in a subshell environment, and its output is used as the possible completions.
  • Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment