Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting
Prev |  | Next

# Chapter 21. Subshells

Running a shell script launches a new process, a subshell.

A subshell is a separate instance of the command processor -- the shell that gives you the prompt at the console or in an xterm window. Just as your commands are interpreted at the command-line prompt, similarly does a script batch-process a list of commands. Each shell script running is, in effect, a subprocess (child process) of the parent shell.

A shell script can itself launch subprocesses. These subshells let the script do parallel processing, in effect executing multiple subtasks simultaneously.

       1 #!/bin/bash
       2 # subshell-test.sh
       3
       4 (
       5 # Inside parentheses, and therefore a subshell . . .
       6 while [ 1 ]   # Endless loop.
       7 do
       8   echo "Subshell running . . ."
       9 done
      10 )
      11
      12 #  Script will run forever,
      13 #+ or at least until terminated by a Ctl-C.
      14
      15 exit $?  # End of script (but will never get here).
      16
      17
      18
      19 Now, run the script:
      20 sh subshell-test.sh
      21
      22 And, while the script is running, from a different xterm:
      23 ps -ef | grep subshell-test.sh
      24
      25 UID       PID   PPID  C STIME TTY      TIME     CMD
      26 500       2698  2502  0 14:26 pts/4    00:00:00 sh subshell-test.sh
      27 500       2699  2698 21 14:26 pts/4    00:00:24 sh subshell-test.sh
      28
      29           ^^^^
      30
      31 Analysis:
      32 PID 2698, the script, launched PID 2699, the subshell.
      33
      34 Note: The "UID ..." line would be filtered out by the "grep" command,
      35 but is shown here for illustrative purposes.

In general, an external command in a script forks off a subprocess, [1] whereas a Bash builtin does not. For this reason, builtins execute more quickly and use fewer system resources than their external command equivalents.

Command List within Parentheses

( command1; command2; command3; ... )

  A command list embedded between `parentheses` runs as a subshell.

Variables in a subshell are not visible outside the block of code in the subshell. They are not accessible to the parent process, to the shell that launched the subshell. These are, in effect, variables local to the child process.

Example 21-1. Variable scope in a subshell

       1 #!/bin/bash
       2 # subshell.sh
       3
       4 echo
       5
       6 echo "We are outside the subshell."
       7 echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
       8 # Bash, version 3, adds the new         $BASH_SUBSHELL variable.
       9 echo; echo
      10
      11 outer_variable=Outer
      12 global_variable=
      13 #  Define global variable for "storage" of
      14 #+ value of subshell variable.
      15
      16 (
      17 echo "We are inside the subshell."
      18 echo "Subshell level INSIDE subshell = $BASH_SUBSHELL"
      19 inner_variable=Inner
      20
      21 echo "From inside subshell, \"inner_variable\" = $inner_variable"
      22 echo "From inside subshell, \"outer\" = $outer_variable"
      23
      24 global_variable="$inner_variable"   #  Will this allow "exporting"
      25                                     #+ a subshell variable?
      26 )
      27
      28 echo; echo
      29 echo "We are outside the subshell."
      30 echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
      31 echo
      32
      33 if [ -z "$inner_variable" ]
      34 then
      35   echo "inner_variable undefined in main body of shell"
      36 else
      37   echo "inner_variable defined in main body of shell"
      38 fi
      39
      40 echo "From main body of shell, \"inner_variable\" = $inner_variable"
      41 #  $inner_variable will show as blank (uninitialized)
      42 #+ because variables defined in a subshell are "local variables".
      43 #  Is there a remedy for this?
      44 echo "global_variable = "$global_variable""  # Why doesn't this work?
      45
      46 echo
      47
      48 # =======================================================================
      49
      50 # Additionally ...
      51
      52 echo "-----------------"; echo
      53
      54 var=41                                                 # Global variable.
      55
      56 ( let "var+=1"; echo "\$var INSIDE subshell = $var" )  # 42
      57
      58 echo "\$var OUTSIDE subshell = $var"                   # 41
      59 #  Variable operations inside a subshell, even to a GLOBAL variable
      60 #+ do not affect the value of the variable outside the subshell!
      61
      62
      63 exit 0
      64
      65 #  Question:
      66 #  --------
      67 #  Once having exited a subshell,
      68 #+ is there any way to reenter that very same subshell
      69 #+ to modify or access the subshell variables?

See also $BASHPID and Example 34-2.

While the $BASH_SUBSHELL internal variable indicates the nesting level of a subshell, the $SHLVL variable shows no change within a subshell.

       1 echo " \$BASH_SUBSHELL outside subshell       = $BASH_SUBSHELL"           # 0
       2   ( echo " \$BASH_SUBSHELL inside subshell        = $BASH_SUBSHELL" )     # 1
       3   ( ( echo " \$BASH_SUBSHELL inside nested subshell = $BASH_SUBSHELL" ) ) # 2
       4 # ^ ^                           *** nested ***                        ^ ^
       5
       6 echo
       7
       8 echo " \$SHLVL outside subshell = $SHLVL"       # 3
       9 ( echo " \$SHLVL inside subshell  = $SHLVL" )   # 3 (No change!)

Directory changes made in a subshell do not carry over to the parent shell.

Example 21-2. List User Profiles

       1 #!/bin/bash
       2 # allprofs.sh: Print all user profiles.
       3
       4 # This script written by Heiner Steven, and modified by the document author.
       5
       6 FILE=.bashrc  #  File containing user profile,
       7               #+ was ".profile" in original script.
       8
       9 for home in `awk -F: '{print $6}' /etc/passwd`
      10 do
      11   [ -d "$home" ] || continue    # If no home directory, go to next.
      12   [ -r "$home" ] || continue    # If not readable, go to next.
      13   (cd $home; [ -e $FILE ] && less $FILE)
      14 done
      15
      16 #  When script terminates, there is no need to 'cd' back to original directory,
      17 #+ because 'cd $home' takes place in a subshell.
      18
      19 exit 0

A subshell may be used to set up a "dedicated environment" for a command group.

       1 COMMAND1
       2 COMMAND2
       3 COMMAND3
       4 (
       5   IFS=:
       6   PATH=/bin
       7   unset TERMINFO
       8   set -C
       9   shift 5
      10   COMMAND4
      11   COMMAND5
      12   exit 3 # Only exits the subshell!
      13 )
      14 # The parent shell has not been affected, and the environment is preserved.
      15 COMMAND6
      16 COMMAND7

As seen here, the exit command only terminates the subshell in which it is running, not the parent shell or script.

One application of such a "dedicated environment" is testing whether a variable is defined.

       1 if (set -u; : $variable) 2> /dev/null
       2 then
       3   echo "Variable is set."
       4 fi     #  Variable has been set in current script,
       5        #+ or is an an internal Bash variable,
       6        #+ or is present in environment (has been exported).
       7
       8 # Could also be written [[ ${variable-x} != x || ${variable-y} != y ]]
       9 # or                    [[ ${variable-x} != x$variable ]]
      10 # or                    [[ ${variable+x} = x ]]
      11 # or                    [[ ${variable-x} != x ]]

Another application is checking for a lock file:

       1 if (set -C; : > lock_file) 2> /dev/null
       2 then
       3   :   # lock_file didn't exist: no user running the script
       4 else
       5   echo "Another user is already running that script."
       6 exit 65
       7 fi
       8
       9 #  Code snippet by Stéphane Chazelas,
      10 #+ with modifications by Paulo Marcel Coelho Aragao.

+

Processes may execute in parallel within different subshells. This permits breaking a complex task into subcomponents processed concurrently.

Example 21-3. Running parallel processes in subshells

       1 	(cat list1 list2 list3 | sort | uniq > list123) &
       2 	(cat list4 list5 list6 | sort | uniq > list456) &
       3 	# Merges and sorts both sets of lists simultaneously.
       4 	# Running in background ensures parallel execution.
       5 	#
       6 	# Same effect as
       7 	#   cat list1 list2 list3 | sort | uniq > list123 &
       8 	#   cat list4 list5 list6 | sort | uniq > list456 &
       9
      10 	wait   # Don't execute the next command until subshells finish.
      11
      12 	diff list123 list456

Redirecting I/O to a subshell uses the "|" pipe operator, as in `ls -al | (command)`.

A code block between curly brackets does not launch a subshell.

{ command1; command2; command3; . . . commandN; }

       1 var1=23
       2 echo "$var1"   # 23
       3
       4 { var1=76; }
       5 echo "$var1"   # 76

### Notes

[1] | An external command invoked with an exec does not (usually) fork off a subprocess / subshell.

Prev | Home | Next
Applications | Up | Restricted Shells