## 24.4. Subshells

In Unix, when a program starts another program (more exactly, when a process starts another process), the new process runs as a subprocess (Section 24.3) or child process.[70] When a shell starts another shell, the new shell is called a subshell.[71]

> [70]This isn't true when the subprocess is execd from the parent process without a fork first. Section 24.2 explains.

> [71]When you use the shell's exec (Section 35.5) command, it does not start a subprocess.

So what? There are some important things to know about it: the child process gets a copy of its parent's environment, and any changes in the environment of the child process aren't passed to its parent. "Still," I hear you say, "so what??"

- Shell scripts are run in a subshell (unless you use the source or . commands (Section 35.29) to start the script). If the script makes changes to the environment of its (sub)shell, the parent shell won't see those changes. If the script uses cd, it doesn't change the current directory in the parent shell. If the script changes the value of the TZ (or any) environment variable, that won't change TZ in the parent shell. The script can set a different umask than the parent shell -- no problem.

- There are times you might want to start a subshell from your current shell. Maybe you have a special project where you need to work in a different current directory, reset environment variables, set a new home directory, reset some aliases, use a different PATH (Section 35.6), whatever. When you end the subshell, the parent shell's environment will be the way it was.

  If your parent shell has job control (Section 23.3), you can stop the subshell and pop back to your parent shell without losing the changes in the subshell. If the child shell has job control, too, the command suspend (or kill -STOP $$ (Section 27.17)) will stop it. Otherwise, just type CTRL-z at the subshell's prompt. For example:

  `prompt` Section 4.1

  >     myprompt% csh
  >     myprompt% set prompt="project% "
  >     project% cd project-directory
  >     project% setenv PRINTER plotter
  >     project% set path=($path some-new-directories)
  >     project% setenv EXINIT "se ts=4 sw=4 aw wm=0"
  >        ...do some work...
  >     project% suspend

  >     Stopped
  >        ...back to parent shell...
  >     myprompt%
  >     myprompt% fg %csh
  >        ...back to subshell...
  >     project%

  I use suspend so much that I've made a CTRL-z-like alias named z:

  >     alias z suspend        ...csh
  >     alias z=suspend        ...bash, ksh

- If you need a different type of shell temporarily, just type that shell's name at a prompt. When you end the shell by typing exit or by suspending it (as shown above), you're back to your usual shell. For example, you might normally use bash but want to use the zsh multiline editing for a few loops you need to run. As another example, I started a lot of different shells while I was writing this book -- and suspended them, as above, when I wasn't using them. Very handy.

- A shell escape (Section 17.21) starts a subshell. Do whatever you want to the subshell's environment. When you end the shell escape, the changes go away.

- The su command starts a subshell. cd anywhere, change environment variables, and so on.

If you use the exit command, a subshell (or any shell) will terminate. In a script, when the shell reads the end of file, that does an implicit exit. On the command line, an end-of-input character (usually CTRL-d) will do the same thing. Section 35.16 explains how exit sets a shell's exit status.

-- JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.