Chapter 20. 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 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, local variables.


Example 20-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 
  47 echo
  48 
  49 exit 0
  50 
  51 #  Question:
  52 #  --------
  53 #  Once having exited a subshell,
  54 #+ is there any way to reenter that very same subshell
  55 #+ to modify or access the subshell variables?

See also Example 31-2.

Note

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 20-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 20-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).

Note

A command block between curly braces does not launch a subshell.

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

Notes

[1]

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

AskApache Web Development