⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02_02.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 2] 2.2 Shell Setup Files-Which, Where, and Why </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:31:51Z"><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="ch02_01.htm"TITLE="2. Logging In"><LINKREL="prev"HREF="ch02_01.htm"TITLE="2.1 Customizing the Shell "><LINKREL="next"HREF="ch02_03.htm"TITLE="2.3 What Goes in Shell Setup Files? "></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="ch02_01.htm"TITLE="2.1 Customizing the Shell "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 2.1 Customizing the Shell "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 2<BR>Logging In</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch02_03.htm"TITLE="2.3 What Goes in Shell Setup Files? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 2.3 What Goes in Shell Setup Files? "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1070">2.2 Shell Setup Files-Which, Where, and Why </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2744"></A><ACLASS="indexterm"NAME="UPT-ART-1070-IX-CONFIGURATION-FILES-SHELLS"></A>To understand setup files, you need to understand that a shell can run intwo modes:as a <EMCLASS="emphasis">login shell</EM> or a <EMCLASS="emphasis">non-login shell</EM>.<ACLASS="indexterm"NAME="AUTOID-2752"></A><ACLASS="indexterm"NAME="AUTOID-2754"></A></P><PCLASS="para">When you log in to a UNIX system, the <EMCLASS="emphasis">login</EM> program usually starts a shell for you.The <EMCLASS="emphasis">login</EM> program sets<SPANCLASS="link">a special flag (<ACLASS="linkend"HREF="ch51_09.htm"TITLE='Making a "Login&quot; Shell'>51.9</A>)</SPAN>to tell a shell that it's a login shell.</P><PCLASS="para">If the shell doesn't have that flag set, it won't act like a login shell.Opening a new window in a window system may or may notstart a login shell&nbsp;- that depends on the configuration.(For example, the command <EMCLASS="emphasis">xterm&nbsp;-ls</EM> starts a login shell in an<SPANCLASS="link"><EMCLASS="emphasis">xterm</EM> window (<ACLASS="linkend"HREF="ch01_31.htm"TITLE="The X Window System ">1.31</A>)</SPAN>;<EMCLASS="emphasis">xterm&nbsp;+ls</EM> starts a non-login shell.)When you connect to a system with programs like <EMCLASS="emphasis">ftp</EM> and <EMCLASS="emphasis">uucp</EM>,that usually starts a non-login shell.And a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>is never a login shell.</P><PCLASS="para">How can you tell whether your shell is a login shell?Unfortunately for newcomers, the answer is: &quot;it depends.&quot;The scheme does make sense&nbsp;- after you get familiar with it, anyway.When you first log in to a system, you want a login shell that setsthings like the <SPANCLASS="link">terminal type (<ACLASS="linkend"HREF="ch05_02.htm"TITLE="The Idea of a Terminal Database ">5.2</A>, <ACLASS="linkend"HREF="ch05_03.htm"TITLE="Setting the Terminal Type When You Log In ">5.3</A>)</SPAN>.<ACLASS="indexterm"NAME="AUTOID-2770"></A>Other shells on the same terminal should be non-login shells&nbsp;- to avoidredoing those one-time-only setup commands.Different shells have their own methods for handling first-time shellinvocations vs. later invocations, and that's what the rest of thisarticle is about.</P><PCLASS="para">Finally, at the risk of really getting ahead of myself:in all the shells I know of,<ACLASS="indexterm"NAME="AUTOID-2773"></A><SPANCLASS="link">parenthesis operators (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>don't read any setup file.Instead, they start another instance of your current shell.<ACLASS="indexterm"NAME="AUTOID-2777"></A>Parentheses are called &quot;subshell operators,&quot; but the subshell theystart doesn't print a prompt and usually has a short lifetime.</P><PCLASS="para">Whew.Read on (I recommend that you read about all of the shells).Then experiment with your shell's setup files until you get thingsworking the way you want them.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1070-SECT-1.1">2.2.1 Bourne Shell </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2783"></A><ACLASS="indexterm"NAME="AUTOID-2785"></A>The original Bourne shell has one file that it reads when you log in:it's called <EMCLASS="emphasis">.profile</EM> and is in your home directory.Put all your setup commands there.</P><PCLASS="para">The Bourne shell doesn't read <EMCLASS="emphasis">.profile</EM> when you start a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>,though.Subshell setup information has to come from<SPANCLASS="link">environment variables (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>that were setin <EMCLASS="emphasis">.profile</EM> when you first logged in or from commands you typed since.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1070-SECT-1.2">2.2.2 C Shell </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2797"></A>C shell users have three shell setup files available:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">The <EMCLASS="emphasis">.cshrc</EM> file is read any time a C shell starts&nbsp;- that includesshell escapes and shell scripts.[1]This is the place to putcommands that should run every time you start a shell.For instance, shell variables like<SPANCLASS="link"><EMCLASS="emphasis">cdpath</EM> (<ACLASS="linkend"HREF="ch14_05.htm"TITLE="Saving Time When You Change Directories: cdpath ">14.5</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">prompt</EM> (<ACLASS="linkend"HREF="ch07_01.htm#UPT-ART-4820"TITLE="Why Change Your Prompt? ">7.1</A>)</SPAN>should be set here.<SPANCLASS="link">Aliases (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>should, too.Those things aren't passed to subshells through the environment, sothey belong in <EMCLASS="emphasis">.cshrc</EM>.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] If you write a <EMCLASS="emphasis">csh</EM> script, you should probably use the<EMCLASS="emphasis">-f</EM> optionto keep C shell scripts fromreading <EMCLASS="emphasis">.cshrc</EM>.Of course, even better, you probably<SPANCLASS="link">shouldn't use <EMCLASS="emphasis">csh</EM> for scripts (<ACLASS="linkend"HREF="ch47_02.htm"TITLE="C Shell Programming Considered Harmful ">47.2</A>)</SPAN>.</P></BLOCKQUOTE></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2818"></A><ACLASS="indexterm"NAME="AUTOID-2820"></A>The <EMCLASS="emphasis">.login</EM> file is read when you start a login shell.Here's where you should set:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2826"></A><SPANCLASS="link">Environment variables (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>(which UNIX will pass to subshells automatically)</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2832"></A>Commands like<SPANCLASS="link"><EMCLASS="emphasis">tset</EM> (<ACLASS="linkend"HREF="ch05_03.htm"TITLE="Setting the Terminal Type When You Log In ">5.3</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">stty</EM> (<ACLASS="linkend"HREF="ch05_09.htm"TITLE="Setting Your Erase, Kill, and Interrupt Characters ">5.9</A>, <ACLASS="linkend"HREF="ch41_03.htm"TITLE="Find Out Terminal Settings with stty ">41.3</A>)</SPAN></P></LI><LICLASS="listitem"><PCLASS="para">Commands you want to run every time you log in&nbsp;- checking for<SPANCLASS="link">mail and news (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,running<SPANCLASS="link"><EMCLASS="emphasis">fortune</EM> (<ACLASS="linkend"HREF="ch03_03.htm"TITLE="Electronic Fortune Cookies ">3.3</A>)</SPAN>,checking your calendar for the day, etc.</P></LI></UL><PCLASS="para">Note that <EMCLASS="emphasis">.cshrc</EM> is read before <EMCLASS="emphasis">.login</EM>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2849"></A>The shell reads <EMCLASS="emphasis">.logout</EM> when you end a login shell.Article<ACLASS="xref"HREF="ch02_07.htm"TITLE="C Shell Setup Files Aren't Read When You Want Them to Be? ">2.7</A>has tips for reading <EMCLASS="emphasis">.logout</EM> from non-login shells.</P></LI></UL></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1070-SECT-1.3">2.2.3 Korn Shell </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2857"></A><ACLASS="indexterm"NAME="AUTOID-2860"></A>The Korn shell is a lot like the Bourne shell.A<SPANCLASS="link">login Korn shell (<ACLASS="linkend"HREF="ch02_08.htm"TITLE="Identifying Login Shells ">2.8</A>)</SPAN>will read the <EMCLASS="emphasis">.profile</EM> first.The <EMCLASS="emphasis">.profile</EM> can set the<SPANCLASS="link"><EMCLASS="emphasis">ENV</EM> (<ACLASS="linkend"HREF="ch06_03.htm"TITLE="Predefined Environment Variables ">6.3</A>)</SPAN>environment variable to the pathname of a file(typically <EMCLASS="emphasis">$HOME/.kshrc</EM>).Thenany Korn shell during that login session (including a subshell)will read the file named by <CODECLASS="literal">$ENV</CODE> as it starts up, before it runsother commands.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1070-SECT-1.4">2.2.4 bash </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2872"></A><EMCLASS="emphasis">bash</EM> is something of a cross between the Bourne and C shells.A login <EMCLASS="emphasis">bash</EM> will read <EMCLASS="emphasis">.bash_profile</EM>, <EMCLASS="emphasis">.bash_login</EM>, or<EMCLASS="emphasis">.profile</EM>.A <EMCLASS="emphasis">bash</EM> subshell&nbsp;- but not a login shell&nbsp;- will read a file named<EMCLASS="emphasis">.bashrc</EM> in your home directory.The shell reads <EMCLASS="emphasis">.bash_logout</EM> when you end a login shell; you canset a<SPANCLASS="link"><EMCLASS="emphasis">trap</EM> (<ACLASS="linkend"HREF="ch03_02.htm"TITLE="Running Commands at Bourne/Korn Shell Logout ">3.2</A>)</SPAN>to handle non-login shells.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1070-SECT-1.5">2.2.5 tcsh </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2888"></A><ACLASS="indexterm"NAME="AUTOID-2890"></A><EMCLASS="emphasis">tcsh</EM> is like the C shell, with one exception:if you put a file named <EMCLASS="emphasis">.tcshrc</EM> in your home directory,<EMCLASS="emphasis">tcsh</EM> will read it instead of <EMCLASS="emphasis">.cshrc</EM>.</P></DIV><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="ch02_01.htm"TITLE="2.1 Customizing the Shell "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 2.1 Customizing the Shell "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="ch02_03.htm"TITLE="2.3 What Goes in Shell Setup Files? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 2.3 What Goes in Shell Setup Files? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">2.1 Customizing the Shell </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">2.3 What Goes in Shell Setup Files? </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 &amp; 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 + -