Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash subshells and command groups

Reference for Bash subshells and command groups: how ( list ) and { list; } differ, what a child shell inherits, variable scope, and when each form is useful.

A subshell is a child copy of the current shell. Writing ( list ) runs the commands in such a copy: they see all your variables, functions and the current directory, but nothing they change survives. A command group { list; } runs the commands in the current shell and only bundles them so that a redirection or a pipe can apply to all of them at once.

Choosing between the two is mostly a question of scope: do you want the changes to stay, or do you want a throwaway sandbox?

Terminal
( cd /tmp && do_something )     # subshell: cd does not leak out
{ echo one; echo two; } > out   # group: one redirection for both commands

Try it

Bash Subshell changes vanish, group changes stay

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Grouping output and the pipeline trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
( list )          # subshell, no trailing semicolon needed
{ list; }         # group: space after {, and a ; or newline before }
$( list )         # command substitution, also runs in a subshell
Name Description
( list ) Runs list in a forked child shell. The exit status is that of the last command.
{ list; } Runs list in the current shell. { and } are reserved words, so they must be separated from the commands by whitespace, and the list must end with ;, & or a newline.
(( expr )) Not a subshell: arithmetic evaluation. Do not confuse it with ( ( list ) ).

Returns: the exit status of the last command in the list.

Errors and edge cases: forgetting the final ; in { echo hi } leaves the group unclosed and Bash waits for more input (or reports a syntax error in a script).

Usage notes

  • What a subshell inherits. Variables (including unexported ones), functions, aliases, options, traps that were set to ignore, open file descriptors and the working directory are copied. It does not share them back afterwards.
  • Where subshells appear. Besides ( ), they are created by each stage of a pipeline, command substitution $( ), process substitution, and background jobs started with &.
  • Use ( cd dir && cmd ) to run a command somewhere else without changing the caller's directory; no cd - bookkeeping is needed.
  • exit in a subshell leaves only the subshell, which is handy for isolating a failure; in a group it exits the whole script.
  • $$ stays the parent's PID inside a subshell. Use $BASHPID for the real PID of the current process, and BASH_SUBSHELL for the nesting depth.
  • Performance. A subshell is a fork. It is cheap, but inside a loop that runs thousands of times, $( ) for trivial work (such as $(echo "$x")) adds up. Prefer parameter expansion.
  • Traps. A subshell resets traps that run commands (they fall back to the default), but a trap that ignores a signal remains ignored.
  • Brace form needs the separator. { ls; } works, {ls;} and { ls } do not.