Parentheses run commands in a subshell. Variable changes inside the subshell do not overwrite the parent shell's variables.

Program

Play the script to compare the value inside a subshell with the value after it exits.

start
subshell_isolation.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

start=5
value=$start
(
    value=$((value + 10))
    echo "inside=$value"
)
echo "outside=$value"
#!/usr/bin/env bash

start=7
value=$start
(
    value=$((value + 10))
    echo "inside=$value"
)
echo "outside=$value"
#!/usr/bin/env bash

start=9
value=$start
(
    value=$((value + 10))
    echo "inside=$value"
)
echo "outside=$value"
  1. start ← 5

    3start=54value=$start
    values this step5start
  2. value ← 5

    3start=54value=$start5(
    values this step5value5start
  3. subshell value ← 15

    5(6    value=$((value + 10))7    echo "inside=$value"
    values this step15subshell value5value
  4. echo "inside=$value"

    6    value=$((value + 10))7    echo "inside=$value"8)
    outputinside=15
    values this step15subshell value
  5. echo "outside=$value"

    8)9echo "outside=$value"
    outputoutside=5
    values this step5value
  1. start ← 7

    3start=74value=$start
    values this step7start
  2. value ← 7

    3start=74value=$start5(
    values this step7value7start
  3. subshell value ← 17

    5(6    value=$((value + 10))7    echo "inside=$value"
    values this step17subshell value7value
  4. echo "inside=$value"

    6    value=$((value + 10))7    echo "inside=$value"8)
    outputinside=17
    values this step17subshell value
  5. echo "outside=$value"

    8)9echo "outside=$value"
    outputoutside=7
    values this step7value
  1. start ← 9

    3start=94value=$start
    values this step9start
  2. value ← 9

    3start=94value=$start5(
    values this step9value9start
  3. subshell value ← 19

    5(6    value=$((value + 10))7    echo "inside=$value"
    values this step19subshell value9value
  4. echo "inside=$value"

    6    value=$((value + 10))7    echo "inside=$value"8)
    outputinside=19
    values this step19subshell value
  5. echo "outside=$value"

    8)9echo "outside=$value"
    outputoutside=9
    values this step9value

Follow the Subshell

  1. start begins as 5.
  2. The parent shell sets value=5.
  3. Inside the subshell, value becomes 15.
  4. The subshell prints inside=15.
  5. After the subshell exits, the parent still has value=5, so it prints outside=5. | place | value | output | | --- | --- | --- | | parent before subshell | 5 | - | | inside subshell | 15 | inside=15 | | parent after subshell | 5 | outside=5 |
subshell `( ... )` runs commands in a child shell process.
isolation Assignments inside the subshell do not change the parent shell.
parent value After the subshell finishes, the parent `value` is still the original value.

Exercise: subshell_isolation.sh

Reproduce inside=15 and outside=5, then use the pinned start variants 7 and 9 to predict inside=17 outside=7 and inside=19 outside=9.