📄 ch06_01.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 6] Shell and Environment Variables</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-10-23T15:37:54Z"><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="part01.htm"TITLE="I. Making Yourself at Home "><LINKREL="prev"HREF="ch05_12.htm"TITLE="5.12 Initializing the Terminal with tput "><LINKREL="next"HREF="ch06_02.htm"TITLE="6.2 Parent-Child Relationships "></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="ch05_12.htm"TITLE="5.12 Initializing the Terminal with tput "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 5.12 Initializing the Terminal with tput "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 6</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch06_02.htm"TITLE="6.2 Parent-Child Relationships "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 6.2 Parent-Child Relationships "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-6">6. Shell and Environment Variables</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-1170"TITLE="6.1 What Environment Variables Are Good For ">What Environment Variables Are Good For </A><BR><ACLASS="sect1"HREF="ch06_02.htm"TITLE="6.2 Parent-Child Relationships ">Parent-Child Relationships </A><BR><ACLASS="sect1"HREF="ch06_03.htm"TITLE="6.3 Predefined Environment Variables ">Predefined Environment Variables </A><BR><ACLASS="sect1"HREF="ch06_04.htm"TITLE="6.4 The PATH Environment Variable ">The PATH Environment Variable </A><BR><ACLASS="sect1"HREF="ch06_05.htm"TITLE="6.5 PATH and path ">PATH and path </A><BR><ACLASS="sect1"HREF="ch06_06.htm"TITLE="6.6 The TZ Environment Variable ">The TZ Environment Variable </A><BR><ACLASS="sect1"HREF="ch06_07.htm"TITLE="6.7 What Time Is It in Japan? ">What Time Is It in Japan? </A><BR><ACLASS="sect1"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">Shell Variables </A><BR><ACLASS="sect1"HREF="ch06_09.htm"TITLE="6.9 Special C Shell Variables ">Special C Shell Variables </A><BR><ACLASS="sect1"HREF="ch06_10.htm"TITLE="6.10 Running a Command with a Temporarily Different Environment ">Running a Command with a Temporarily Different Environment </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1170">6.1 What Environment Variables Are Good For </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-5707"></A>Many UNIX utilities, including the shell, need information about you and what you're doing in order to do a reasonable job.</P><PCLASS="para">What kinds of information?Well, to start with, a lot of programs (particularly editors)need to know what kind of terminal you're using. The shell needs toknow where any commands you want to use are likely to be found. Lotsof UNIX programs (like mail programs)include a command to start an editor as a subprocess;they like to know your favorite editor. And so on.</P><PCLASS="para">Of course, one could always write programs that made you put all thisinformation on the command line. For example, you might have to typecommands like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mail -editor vi -term aardvark48 -favoritecolor blue_no_red</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">But your favorite editor probably doesn't change every day. (Nor willyour favorite color.) The terminal you use may change frequently, butit certainly won't change from the time you log in until the time youlog out. And you certainly wouldn't want to type something like thiswhenever you want to send mail.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-5717"></A>Rather than forcing you to type this information with every command,UNIX uses <EMCLASS="emphasis">environment variables</EM> to store information that you'drather not worry about.For example, the <SPANCLASS="link"><EMCLASS="emphasis">TERM</EM> (<ACLASS="linkend"HREF="ch05_10.htm"TITLE="Finding What Terminal Names You Can Use ">5.10</A>)</SPAN>environment variable tells programs what kind ofterminal you're using. Any programs that care about your terminaltype know (or ought to know) that they can read this variable, find out your terminaltype, and act accordingly.</P><PCLASS="para">Similarly, the directories that store the commands you wantto execute are listed in the <SPANCLASS="link"><EMCLASS="emphasis">PATH</EM> (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>)</SPAN><ACLASS="indexterm"NAME="AUTOID-5725"></A>variable. When you type a command,your shell looks through each directory in your <EMCLASS="emphasis">PATH</EM> variable to findthat command. Presumably, UNIX wouldn't need a <EMCLASS="emphasis">PATH</EM> variable if all commands were located in the same directory; but you'll soon bewriting your own commands (if you aren't already), and storing them inyour own<SPANCLASS="link">"private" command directories (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>,and you'll need to tell the shell<SPANCLASS="link">how to find them (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="xref.warning"HREF="ch06_02.htm"TITLE="6.2 Parent-Child Relationships ">Warning!</A> </TH><TDVALIGN="TOP">Environment variables are managed by your shell. The differencebetween environment variables and regular <SPANCLASS="link">shell variables (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>is that a shell variable is local to a particular instance of the shell(such as a shell script), while environment variables are "inherited"by any program you start, including<SPANCLASS="link">another shell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>.That is, the newprocess gets its own copy of these variables, which it can read,modify, and pass on in turn to its own children.In fact, every UNIX process (not just the shell) passes its environmentvariables to its child processes.</TD></TR></TABLE><PCLASS="para">You can set environment variables with a command like this:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch08_05.htm"TITLE="8.5 Command-Line Evaluation ">;</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>setenv </B></CODE><CODECLASS="replaceable"><I>NAME value </I></CODE><EMCLASS="emphasis">C shell</EM>$ <CODECLASS="replaceable"><I>NAME</I></CODE><CODECLASS="userinput"><B>=</B></CODE><CODECLASS="replaceable"><I>value</I></CODE><CODECLASS="userinput"><B>; export </B></CODE><CODECLASS="replaceable"><I>NAME </I></CODE><EMCLASS="emphasis">Bourne or Korn shell</EM> </PRE></TD></TR></TABLE></P><PCLASS="para">There's nothing particularly special about the <EMCLASS="emphasis">NAME</EM>; you can createenvironment variables with any names you want. Of course, thesedon't necessarily do anything for you; variables like <EMCLASS="emphasis">PATH</EM> and <EMCLASS="emphasis">TERM</EM>are important because lots of programs have<SPANCLASS="link">"agreed" (<ACLASS="linkend"HREF="ch06_03.htm"TITLE="Predefined Environment Variables ">6.3</A>)</SPAN>that these namesare important. But if you want to create an environment variable thatholds the name of your lover, that's your business:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>setenv LOVER Judy</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you're so inclined, you could write a program called<EMCLASS="emphasis">valentine</EM> that reads the <EMCLASS="emphasis">LOVER</EM> environment variable and generates anappropriate message. If you like short-term relationships or tend toforget names, this might even be convenient!</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-5761"></A>By convention, the names of environment variables use all uppercase letters. There's nothing to enforce this convention - if you're makingyour own names, you can use any capitalization you please. Butthere's no advantage to violating the convention, either. Theenvironment variables that are used by standard UNIX programs all haveuppercase names.[I usually make my shell variable names lowercase so it's easy totell the difference. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-5766"></A><ACLASS="indexterm"NAME="AUTOID-5768"></A><ACLASS="indexterm"NAME="AUTOID-5770"></A>If you want the C shell to forget that an environment variable everexisted, use the command <EMCLASS="emphasis">unsetenv NAME</EM>.(Some Bourne shells, but not all, have a similar command:<EMCLASS="emphasis">unset NAME</EM>.)</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="printenv">printenv</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="env">env</A><BR></TH><TDVALIGN="TOP"> If you want to list all of your environment variables, use<EMCLASS="emphasis">printenv</EM> or <EMCLASS="emphasis">env</EM>.(Both are on the CD-ROM.)The <EMCLASS="emphasis">printenv</EM> command also lets you ask about a particular variable.Here's a typical report:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>printenv EDITOR</B></CODE>EDITOR=/usr/local/bin/emacs% <CODECLASS="userinput"><B>printenv</B></CODE>HOME=/home/los/mikelSHELL=/bin/cshTERM=sunUSER=mikelPATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin:.:/home/los/mikel/binLOGNAME=mikelPWD=/home/los/mikel/power/articlesPRINTER=psEDITOR=/usr/local/bin/emacs</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-5785"></A>The <SPANCLASS="link"><EMCLASS="emphasis">set</EM> (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>command provides a similar listing of shell variables.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-5791"></A>You can also use the<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>command to show the value of a particularvariable, preceding the variable name with a dollar sign (which tellsthe shell to substitute the value of the variable):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo $TERM</B></CODE>xterm</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></DIV></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="ch05_12.htm"TITLE="5.12 Initializing the Terminal with tput "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 5.12 Initializing the Terminal with tput "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="ch06_02.htm"TITLE="6.2 Parent-Child Relationships "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 6.2 Parent-Child Relationships "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">5.12 Initializing the Terminal with tput </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">6.2 Parent-Child Relationships </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 + -