home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 38.3 Managing Processes: Overall Concepts Chapter 38
Starting, Stopping, and Killing Processes
Next: 38.5 The ps Command
 

38.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 ( 38.3 ) or child process. [1] When a shell starts another shell, the new shell is called a subshell . [2]

[1] This isn't true when the subprocess is exec d from the parent process without a fork first. Article 38.2 explains.

[2] When you use the shell's exec ( 45.7 ) 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. 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 ( 44.23 ) 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 ( 6.7 ) , that won't change TZ in the parent shell. The script can set a different umask ( 22.4 ) 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 ( 6.4 ) , whatever. When you end the subshell, the parent shell's environment will be the way it was.

    If your parent shell has job control ( 12.1 ) , 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 ( 22.22 ) (or kill -STOP $$ ( 8.19 ) ) will stop it. Otherwise, just type CTRL-z at the subshell's prompt. For example:

    
    
    prompt
     
    
    
    
    
    
    
    
    
    
    
    
    %csh
     
    
    
    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
    myprompt%
       
    ...back to parent shell...
    
    myprompt% 
    
    fg %csh
    
    
       
    ...back to subshell...
    
    %

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

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

  • The su ( 22.22 ) 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. Article 44.11 explains how exit sets a shell's exit status.

- JP


Previous: 38.3 Managing Processes: Overall Concepts UNIX Power Tools Next: 38.5 The ps Command
38.3 Managing Processes: Overall Concepts Book Index 38.5 The ps Command

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System










??????????????@Mail.ru