$_ CLI In Depth
Learning progress 0%
case functions arrays parameter-expansion

System Deep-Dive: Subshells & Why Variables Vanish

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:16

What a subshell is

A subshell is a copy of the shell process that runs a set of commands and then exits. It starts with the same variables, functions, and working directory as its parent, but any change it makes stays inside the copy. A group in parentheses, a command substitution, and most of the parts of a pipeline run in subshells. A group in braces does not, because it runs in the current shell.

BASH3 lines
x=outer
( x=inner; echo "in parens: $x" )
echo "after parens: $x"

The output is in parens: inner and then after parens: outer. The parentheses created a copy, the change happened in the copy, and the copy went away. Replace the parentheses with braces and the second line changes to inner, because the braces run in the current shell.

Pipelines are the classic trap

In a bash script, every part of a pipeline runs in its own subshell by default, including the last part. That means a read at the end of a pipeline sets a variable that the rest of the script cannot see. The shell option lastpipe changes this, but it works only when job control is off, which is not the case in an interactive terminal, and it is off by default.

BASH2 lines
printf "hello\n" | read v
echo "v=[$v]"

The output is v=[]. The read ran in a subshell and stored hello there, and the parent never received it. The fix is to avoid the pipeline and redirect instead, so that the read runs in the current shell.

BASH2 lines
read v < <(printf "hello\n")
echo "v=[$v]"

The output is v=[hello]. The process substitution feeds the data to read through a file descriptor, and read itself runs in the current shell. The same idea works for loops that read lines, which is why many scripts use while read line; do ...; done < file instead of piping into the loop.

The loop that loses its count

The same trap appears when a loop is fed by a pipe. The loop body runs in a subshell, so a counter that the loop increments is not visible after the loop.

BASH3 lines
count=0
printf "a\nb\nc\n" | while read line; do count=$((count+1)); done
echo "count=$count"

The output is count=0, even though the loop ran three times. The counter was updated inside the pipeline subshell. Use the redirect form, while read line; do ...; done < file or with a process substitution, and the count will be correct.

Warning

A command substitution also runs in a subshell. An assignment inside $(...) does not change the parent variable, and a function called from inside it cannot change globals for the caller.

Changing directory in a subshell

Parentheses are also the simplest way to change directory temporarily. The change of directory happens in the subshell, so the caller keeps its original location. This is a clean pattern for scripts that work in several places.

BASH3 lines
pwd
(cd /tmp && pwd)
pwd

The first and last lines print the same working directory. The middle line prints /tmp, which is visible only inside the parentheses.

What the subshell inherits

A subshell inherits the variables and functions of its parent, but not the changes made after the copy was created. Exported variables are also passed to child programs. Non-exported shell variables are visible in the subshell but not to programs it starts, which is a second place where a value can seem to vanish.

BASH3 lines
local_only=yes
export shared=yes
bash -c 'echo "local=[$local_only] shared=[$shared]"'

The output is local=[] shared=[yes]. The child bash received the exported variable through the environment, but it did not receive the plain shell variable, which lives only in the current shell.

Hands-on: find the subshell

Run the block below and compare the two results. The lab for this unit asks you to record the difference in a file, so you can see it without depending on your interactive state.

BASH5 lines
total=0
for n in 1 2 3; do total=$((total+n)); done
echo "braces total=$total"
(for n in 1 2 3; do total=$((total+n)); done)
echo "parens total=$total"

The first loop runs in the current shell and changes total to 6, so the first line prints 6. The loop in parentheses starts from the value 6 inside its copy and computes 12 there, but the copy is discarded. The second line still prints 6, so the subshell left no trace in the parent.

Why a variable set in a pipeline disappears

Visualizer

Every part of a pipeline runs in its own process, so a while loop at the end of a pipe changes a copy of the variables. Here are three ways to keep the change.

1 / 4
echo xpart 1
while read l; do n=1; donepart 2

The | makes a pipeline. Bash starts each part of a pipeline in its own process, and the loop is one of those parts.

unset n; echo x | while read l; do n=1; done; echo "n=${n-unset}"
Check yourself Question 1 of 4
Concept Check Which construct makes a subshell recall

Which of these constructs runs its commands in a subshell rather than the current shell?

Recap & Key Takeaways

Parentheses, command substitution, and most pipeline stages run in subshells, so variables they set disappear when the subshell exits.
In a script, a read or loop at the end of a pipeline cannot set variables for the rest of the script, so use a redirect or process substitution instead.
Parentheses are a clean way to change directory or variables temporarily without affecting the caller.
Only exported variables reach child programs, while plain shell variables are visible only in the shell that holds them and its subshells.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue