Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 95 ยท Pipes and Redirection

Subshells, Groups and Environment

Understand Bash subshells: ( ) versus { }, variable scope, cd in subshells, export and the environment, bash -c, and the pipeline variable-loss gotcha.

Every time the shell runs something, it has to decide which process runs it. Most of the time the answer is "the current shell". But a surprising number of constructs start a copy of the shell instead: parentheses, $( ), every stage of a pipeline, background jobs &. Inside that copy you can change variables, move to another directory or set options, and none of it affects the shell you started from.

This is one of the biggest sources of "why did my variable disappear?" questions in Bash. In this lesson you will learn exactly what a subshell inherits, what it throws away, how to group commands with ( ) and { }, how export and the environment work, and how to avoid the classic pipeline trap.

Processes, copies and inheritance

A child process is created by fork: the kernel makes a nearly identical copy of the parent. A subshell is a fork of the shell itself, so it starts with a copy of all the parent's variables, functions, options, open files and current directory. A new program (like ls) is started by fork followed by exec, which replaces the copy with the new program. The program only receives the parent's environment variables and its open file descriptors, not the shell's other variables.

TEXT
   parent shell                          child (subshell)
   +---------------------+   fork       +---------------------+
   | x=1  (shell var)    | -----------> | x=1  (a copy)       |
   | cwd=/home/ann       |              | cwd=/home/ann       |
   | functions, options  |              | functions, options  |
   +---------------------+              +---------------------+
          ^                                      |
          |      changes in the child never      |
          +------------- flow back --------------+

The key rule: information flows from parent to child, never back. The only ways for a child to tell the parent something are its output (stdout), its exit status, and files.

Subshells with ( )

A list in parentheses runs in a subshell:

Bash Changes in a subshell are discarded

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.

The assignment and the cd happened in the copy, so the parent still has name=parent and is still in demo. But the file system is shared: the directory inner was really created. Processes have separate memory but see the same disk.

cd in a subshell: the tidy way to visit another folder

Because the working directory belongs to the process, a subshell is the neat way to run a command "over there" and come back automatically:

Bash Run something in another directory without cd back

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.

The first form cannot leave you in the wrong place, even if ls fails. The second must be undone manually (cd ../..), and if you forget it, later commands run in the wrong directory.

Where subshells hide

You do not have to type parentheses to get a subshell. Bash starts one for each of these:

Construct Notes
( list ) An explicit subshell
$( list ) Command substitution: its output replaces the expression
a | b | c Each stage is its own process (unless lastpipe is on)
list & A background job
<( list ), >( list ) Process substitution
coproc A coprocess
Running a script file Not a subshell, but a brand-new shell process (and a new program)

$BASHPID holds the process ID of the current shell process, including a subshell, while $$ always keeps the ID of the top-level shell. Comparing them reveals whether you are in a subshell. (Process IDs differ from run to run, so the example only prints the comparison.)

Bash Detecting a subshell with BASHPID

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.

Group commands with { }

Braces group commands without creating a subshell. They are used to apply one redirection or one pipeline to several commands:

Bash Braces group commands in the current shell

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.

Two syntax details trip people up. { and } are reserved words, not punctuation, so they need spaces around them, and the last command inside must end with ; or a newline before }. Writing {echo hi} or { echo hi } is a syntax error. Parentheses need neither.

( list ) { list; }
Process Subshell (forked) Current shell
Variable changes survive No Yes
cd survives No Yes
exit Leaves only the subshell Exits the whole shell
Needs spaces/semicolon No Yes
Cost A fork Almost free
Bash exit inside ( ) versus { }

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.

The note under the output shows exit code 4: the script stopped inside the braces, as designed. Use ( ... ) when you want an isolated attempt whose failure or exit must not end your script.

Redirecting a whole group

A group is a single command as far as redirection is concerned. This is the cleanest way to redirect many commands together, including both streams: { cmd1; cmd2; } > out.log 2>&1. See Standard Streams and Redirection.

The environment and export

Variables come in two flavours:

  • Shell variables exist only inside the shell process (and its forked subshells, which copy them).
  • Environment variables are marked with export. They are passed to every program the shell executes (exec), where they appear as that program's environment.
TEXT
   shell variable    GREETING=hi        visible to this shell and ( ) copies
   exported          export GREETING    ALSO passed to every program started
Bash export decides what programs can see

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.

Three useful details:

  • NAME=value command sets a variable for that one command only, and it is automatically exported to it. This is the standard way to pass settings, as in LC_ALL=C sort file.
  • export NAME=value assigns and exports at once. export -n NAME removes the mark. unset NAME removes the variable entirely.
  • env and printenv show the environment. export -p or declare -x list what is exported by the current shell. More about this in Variables and Environment Variables and Environment, Profiles and Configuration.

Exporting is a one-way copy at start-up. If you change an exported variable later, programs that are already running do not notice. Programs you start afterwards get the new value.

bash -c and fresh shells

bash -c 'commands' starts a new shell that runs the string and exits. It is the standard way to hand a small script to xargs, find -exec, su or timeout, and a good tool for experiments: it starts with only the exported environment, so it shows what a script really sees. Arguments after the command string become $0, $1, and so on:

Bash bash -c with arguments

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.

The first bash -c 'hello' fails with bash: hello: command not found. export -f is Bash-specific and puts the function into the environment, so a child Bash can use it. Do not rely on it for portable scripts.

The pipeline gotcha

Because each stage of a pipeline is a subshell, a loop at the end of a pipe cannot update variables of your script. This is the most common subshell bug of all:

Bash Variables set in a pipeline are lost

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.

There are three clean ways out, in order of preference:

  1. Redirect the input into the loop (done < file). It is simple and fast. No extra process.
  2. Process substitution (done < <(command)) when the data comes from a command. See xargs, Command Substitution and Process Substitution.
  3. shopt -s lastpipe runs the last stage of a pipeline in the current shell. It only works when job control is off, which is true in scripts but not in interactive shells.
Bash lastpipe keeps the loop in the main shell

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.

The same rule applies to values you try to return from $( ): result=$(cd /tmp; x=5) does not set x in your shell. If you need several values from a subshell, print them and capture or read the output: for example read -r a b < <(echo 1 2).

Practical patterns

  • Temporary environment for a block: ( export TZ=UTC; set -e; do_stuff ) keeps set -e and TZ from leaking out.
  • Isolated cd: (cd "$dir" && make).
  • Try something risky: ( set -e; step1; step2 ) and then look at $?. Do not put the subshell directly in an if, || or && test: Bash ignores set -e inside any command that is part of a condition, so the steps would not stop at the first failure.
  • Redirect many commands once: { ...; } > log 2>&1.
  • Do not overuse subshells. Each fork costs a little time; thousands of $(...) in a loop are slow. Prefer parameter expansion and built-ins (Parameter Expansion and String Handling).
Bash Putting it together

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.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does this print? x=1; (x=2); echo $x

    The parentheses run in a subshell, a copy of the shell. The assignment changes only the copy, so the main shell still has x=1.
  2. 2. Which statement about { cmd1; cmd2; } is correct?

    Braces group commands in the current shell. They are reserved words, so they need spaces, and the last command must be terminated before }.
  3. 3. A script sets export COLOR=red and later runs bash -c 'echo $COLOR'. Then the script sets COLOR=blue and calls the same command again. What does the second call print?

    The variable stays exported, so assigning a new value updates what future programs receive. A new child process copies the environment when it starts, so it sees blue.
  4. 4. Why does count stay 0 in cat f | while read -r l; do count=$((count+1)); done?

    Each pipeline stage is a separate process. Changes made there never reach the parent. Use done < f, done < <(cmd) or shopt -s lastpipe instead.
  5. 5. What is the cleanest way to run make in another directory and automatically return afterwards?

    The working directory belongs to the subshell process, so it disappears when the parentheses end and your own directory never changes.

Summary

  • A subshell is a forked copy of the shell: it inherits variables, functions and the working directory, and changes inside it never flow back.
  • Subshells are created by ( ), $( ), each pipeline stage, & and process substitution. $BASHPID differs from $$ inside one.
  • { list; } groups commands in the current shell (spaces and a terminating ; required); ( list ) isolates them.
  • exit inside ( ) ends only the subshell; inside { } it ends the shell.
  • Only exported variables (and functions with export -f) reach programs started by the shell; NAME=value cmd sets a variable for one command.
  • bash -c 'string' arg0 arg1 runs a fresh shell and is the way to give small scripts to xargs and friends.
  • The pipeline gotcha: a while loop after a pipe cannot change your variables. Redirect the input, use < <(cmd), or shopt -s lastpipe.

Next: Writing Your First Script