Skip to content
C.W.K.
Stream
Lesson 04 of 05 · published

Subshells and Command Grouping

~8 min · subshell, grouping, scope

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Two ways to group commands

  • { cmd1; cmd2; } — run in current shell. Variables persist after the group.
  • (cmd1; cmd2) — run in a subshell. Variable changes don't leak out. Slightly slower (fork).

When to use {}

Apply a single redirection to several commands:

{
  echo "=== build log ==="
  date
  ./build.sh
} > build.log 2>&1

The whole block writes to build.log. Saves three >> redirections.

When to use ()

Isolate side effects. Common: cd into a dir, do work, come back automatically:

(
  cd ~/projects/temp
  ./make.sh
)
# We're still in the original directory

Variables, cd, set options inside the parens don't affect the parent shell.

Capturing in $()

$(cmd) is also a subshell. That's why x=10; $(x=20) doesn't change x — the assignment lived in a fork.

Watch the syntax for {}

  • Need spaces inside the braces.
  • Need a trailing semicolon (or newline) before the closing brace.

{cmd} doesn't work; { cmd; } does. The shell's parser is picky here.

Isolation is not atomicity

A subshell keeps directory and variable changes from leaking back, but it does not automatically stop after an inner failure. Design the failure flow explicitly.

The final command controls the group's status

An earlier failure followed by a successful echo can make the whole group look successful. Return failures deliberately when the group is used as a gate.

Code

Group + redirect·bash
{
  echo '== status =='
  date
  uname -a
  df -h /
} > /tmp/snapshot.txt 2>&1
cat /tmp/snapshot.txt
Subshell auto-reset·bash
pwd
(
  cd /tmp
  pwd          # /tmp
)
pwd            # back to original

External links

Exercise

Run (cd /tmp; pwd); pwd and confirm you stayed in your original dir. Then { echo a; echo b; } > /tmp/two.txt; cat /tmp/two.txt. Notice the difference.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.