📄 ch38_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 38] 38.4 Subshells </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:49:35Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch38_01.htm"TITLE="38. Starting, Stopping, and Killing Processes"><LINKREL="prev"HREF="ch38_03.htm"TITLE="38.3 Managing Processes: Overall Concepts "><LINKREL="next"HREF="ch38_05.htm"TITLE="38.5 The ps Command "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_03.htm"TITLE="38.3 Managing Processes: Overall Concepts "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.3 Managing Processes: Overall Concepts "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 38<BR>Starting, Stopping, and Killing Processes</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_05.htm"TITLE="38.5 The ps Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.5 The ps Command "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-8020">38.4 Subshells </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42425"></A><ACLASS="indexterm"NAME="AUTOID-42427"></A>In UNIX, when a program starts another program(more exactly, when a process starts another process),the new process runs as a<SPANCLASS="link">subprocess (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>or child process.[1]When a shell starts another shell, the new shell is called a <EMCLASS="emphasis">subshell</EM>.[2]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] This isn't true when the subprocess is <EMCLASS="emphasis">exec</EM>d from the parent processwithout a <EMCLASS="emphasis">fork</EM> first.Article<ACLASS="xref"HREF="ch38_02.htm"TITLE="fork and exec ">38.2</A>explains.</P><PCLASS="para">[2] When you use the shell's<SPANCLASS="link"><EMCLASS="emphasis">exec</EM> (<ACLASS="linkend"HREF="ch45_07.htm"TITLE="The exec Command ">45.7</A>)</SPAN>command, it does not start a subprocess.</P></BLOCKQUOTE><PCLASS="para">So what?There aresome important things to knowabout 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??"</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42445"></A><ACLASS="indexterm"NAME="AUTOID-42447"></A><ACLASS="indexterm"NAME="AUTOID-42450"></A>Shell scripts are run in a subshell (unless you use the<SPANCLASS="link"><EMCLASS="emphasis">source</EM> or <CODECLASS="literal">.</CODE> commands (<ACLASS="linkend"HREF="ch44_23.htm"TITLE="Reading Files with the . and source Commands ">44.23</A>)</SPAN>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 <EMCLASS="emphasis">cd</EM>, it doesn't change the current directoryin the parent shell.If the script<SPANCLASS="link">changes the value of the <EMCLASS="emphasis">TZ</EM> (or any) environment variable (<ACLASS="linkend"HREF="ch06_07.htm"TITLE="What Time Is It in Japan? ">6.7</A>)</SPAN>,that won't change <EMCLASS="emphasis">TZ</EM> in the parent shell.The script can set a different<SPANCLASS="link"><EMCLASS="emphasis">umask</EM> (<ACLASS="linkend"HREF="ch22_04.htm"TITLE="Setting an Exact umask ">22.4</A>)</SPAN>than the parent shell - no problem.</P></LI><LICLASS="listitem"><PCLASS="para">There are times you might want to start a subshell from your currentshell.Maybe you have a special project where you need to work in adifferent current directory, reset environment variables,set a new home directory, reset some aliases, use a different<SPANCLASS="link"><EMCLASS="emphasis">PATH</EM> (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>)</SPAN>,whatever.When you end the subshell, the parent shell's environment will be the wayit was.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42467"></A><ACLASS="indexterm"NAME="AUTOID-42469"></A><ACLASS="indexterm"NAME="AUTOID-42471"></A>If your parent shell has<SPANCLASS="link">job control (<ACLASS="linkend"HREF="ch12_01.htm#UPT-ART-0439"TITLE="Job Control: Work Faster, Stop Runaway Jobs ">12.1</A>)</SPAN>,you can stop the subshell and pop back to your parent shell without losingthe changes in the subshell.If the child shell has job control, too, the command<SPANCLASS="link"><EMCLASS="emphasis">suspend</EM> (<ACLASS="linkend"HREF="ch22_22.htm"TITLE="The su Command Isn't Just for the Superuser ">22.22</A>)</SPAN>(or kill -STOP<SPANCLASS="link">$$ (<ACLASS="linkend"HREF="ch08_19.htm"TITLE='"Special" Characters and Operators '>8.19</A>)</SPAN>) will stop it.Otherwise, just type CTRL-z at the subshell's prompt.For example:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch07_02.htm"TITLE="7.2 Basics of Setting the Prompt ">prompt</A> <ACLASS="co"HREF="ch12_02.htm"TITLE="12.2 Other Ways to Refer to Jobs ">%csh</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">myprompt% <CODECLASS="userinput"><B>csh</B></CODE>myprompt% <CODECLASS="userinput"><B>set prompt="project% "</B></CODE>project% <CODECLASS="userinput"><B>cd </B></CODE><CODECLASS="replaceable"><I>project-directory</I></CODE>project% <CODECLASS="userinput"><B>setenv PRINTER plotter</B></CODE>project% <CODECLASS="userinput"><B>set path=($path </B></CODE><CODECLASS="replaceable"><I>some-new-directories</I></CODE><CODECLASS="userinput"><B>)</B></CODE>project% <CODECLASS="userinput"><B>setenv EXINIT "se ts=4 sw=4 aw wm=0"</B></CODE> <ICLASS="lineannotation">...do some work...</I>project% <CODECLASS="userinput"><B>suspend</B></CODE>Stoppedmyprompt% <ICLASS="lineannotation">...back to parent shell...</I>myprompt% <CODECLASS="userinput"><B>fg %csh</B></CODE> <ICLASS="lineannotation">...back to subshell...</I>%</PRE></TD></TR></TABLE></P><PCLASS="para">I use <EMCLASS="emphasis">suspend</EM> so much that I've made a CTRL-z-like alias named<EMCLASS="emphasis">z</EM>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42501"></A><ACLASS="indexterm"NAME="AUTOID-42504"></A>A<SPANCLASS="link">shell escape (<ACLASS="linkend"HREF="ch30_26.htm"TITLE="Shell Escapes: Running One UNIX Command While Using Another">30.26</A>)</SPAN>starts a subshell.Do whatever you want to the subshell's environment.When you end the shell escape, the changes go away.</P></LI><LICLASS="listitem"><PCLASS="para">The<SPANCLASS="link"><EMCLASS="emphasis">su</EM> (<ACLASS="linkend"HREF="ch22_22.htm"TITLE="The su Command Isn't Just for the Superuser ">22.22</A>)</SPAN>command starts a subshell.<EMCLASS="emphasis">cd</EM> anywhere, change environment variables, and so on...</P></LI></UL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42514"></A><ACLASS="indexterm"NAME="AUTOID-42517"></A>If you use the <EMCLASS="emphasis">exit</EM> command, a subshell (or any shell) will terminate.In a script, when the shell reads the end of file, thatdoes an implicit <EMCLASS="emphasis">exit</EM>.On the command line, an end-of-input character (usually CTRL-d)will do the same thing.Article<ACLASS="xref"HREF="ch44_11.htm"TITLE="Set Exit Status of a Shell (Script) ">44.11</A>explains how <EMCLASS="emphasis">exit</EM> sets a shell's exit status.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_03.htm"TITLE="38.3 Managing Processes: Overall Concepts "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.3 Managing Processes: Overall Concepts "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_05.htm"TITLE="38.5 The ps Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.5 The ps Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">38.3 Managing Processes: Overall Concepts </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">38.5 The ps Command </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -