📄 378-380.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=18//-->
<!--PAGES=378-380//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="376-378.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch19/381-384.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading31"></A><FONT COLOR="#000077">Exporting Variables to the New Shell</FONT></H4>
<P>When you create shell variables or give existing variables values, they exist in the running shell. A variable set in the login shell is available to all command-line arguments. A variable set within a shell has that value only within that shell. The value disappears or is reset when you exit that shell.
</P>
<P>For example, enter these two commands from the command line:</P>
<!-- CODE SNIP //-->
<PRE>
today=Thursday
echo $today
</PRE>
<!-- END CODE SNIP //-->
<P>Suppose that the <TT>echo</TT> command displays <TT>Thursday</TT>. Now suppose that you write and execute the following shell script named <TT>whatday</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
# Name: whatday
# display the current value of the variable today
echo “Today is $today.”
# set the value of today
today=Friday
# display the current value of the variable today
echo “Today is $today.”
</PRE>
<!-- END CODE SNIP //-->
<P>Now enter the following four commands from the command line:
</P>
<!-- CODE SNIP //-->
<PRE>
chmod +x whatday
today=Thursday
whatday
echo $today
</PRE>
<!-- END CODE SNIP //-->
<P>The following lines appear on-screen:
</P>
<!-- CODE SNIP //-->
<PRE>
Today is .
Today is Friday.
Thursday
</PRE>
<!-- END CODE SNIP //-->
<P>The value of the variable <TT>today</TT> in the login shell is <TT>Thursday</TT>. When you execute the shell script <TT>whatday</TT>, you see that initially the variable <TT>today</TT> isn’t defined (as shown by the display <TT>Today is .</TT>). Then the <TT>today</TT> variable has the value <TT>Friday</TT> in the shell. When the <TT>whatday</TT> script terminates, you return to the login shell and <TT>today</TT> has its original value, <TT>Thursday</TT>.</P>
<P>To give the variable <TT>today</TT> the same value that it has in the login shell when the shell script <TT>whatday</TT> starts, use the command <TT>export</TT>. This command “exports,” or passes on, the variables from one shell to subsequent shells:</P>
<!-- CODE SNIP //-->
<PRE>
export today
</PRE>
<!-- END CODE SNIP //-->
<P>Now any shell started from the login shell inherits the value of the variable <TT>today</TT>. Add the <TT>export</TT> command to the preceding sequence of commands:</P>
<!-- CODE SNIP //-->
<PRE>
today=Thursday
export today
whatday
echo $today
</PRE>
<!-- END CODE SNIP //-->
<P>You see the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
Today is Thursday.
Today is Friday.
Thursday
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that the value the variable receives in the shell started by the <TT>whatday</TT> script isn’t carried back to the login shell. Exportation or inheritance of variable values goes in only one direction—from a running shell down to the new shell, never back up. That’s why when you change your current directory inside one shell, you’re back to where you started when that shell terminates.</P>
<P>You can export any variable from one shell down to another shell by using the following syntax:</P>
<!-- CODE SNIP //-->
<PRE>
export variable-name
</PRE>
<!-- END CODE SNIP //-->
<P>In this syntax, <TT>variable-name</TT> is the name of the variable you want to export. To change your terminal type from its current setting to a vt100, for example, enter the following commands to make the new value of <TT>TERM</TT> available to all subsequent shells or programs:</P>
<!-- CODE SNIP //-->
<PRE>
TERM=vt100
export TERM
</PRE>
<!-- END CODE SNIP //-->
<P>When you change or set <TT>bash</TT> shell variables in the .profile file, be sure to export them. For example, if you want the <TT>PATH</TT> variable to be <TT>PATH=/bin:/usr/bin:/usr/local/bin:.</TT>, set it in the .profile file and follow it with this <TT>export</TT> command:</P>
<!-- CODE SNIP //-->
<PRE>
export PATH
</PRE>
<!-- END CODE SNIP //-->
<P>To change the shell prompt, you must set a value for <TT>PS1</TT> in the file .profile. To change it from <TT>$</TT> to <TT>Ready $</TT>, for example, use a text editor to put these lines in the file named .profile:</P>
<!-- CODE SNIP //-->
<PRE>
PS1=“Ready $”
export PS1
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Changes you make to .profile or .login don’t take effect until you log out and log in again.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading32"></A><FONT COLOR="#000077">Defining Command Aliases</FONT></H4>
<P>Command aliases are useful for defining commands you use regularly but for which you don’t want to bother remembering the details. Command aliases are also useful for enhancing your working environment with a set of useful tools. This command assigns the alias <TT>recent</TT> to a command that lists the 10 most recently modified files in the current directory:</P>
<!-- CODE SNIP //-->
<PRE>
alias recent=“ls -lat|head”
</PRE>
<!-- END CODE SNIP //-->
<P>To avoid typing your command aliases each time you log in, put them in the .login file if you’re using the C shell or the .profile file if you’re using <TT>bash</TT> or a similar shell. The command aliases are now available to you when you’re in your shell.</P>
<H3><A NAME="Heading33"></A><FONT COLOR="#000077">From Here…</FONT></H3>
<P>The shell is the primary interface between you and the Linux operating system. Although a shell can be almost any executable program, several standard shells are supplied with Linux or are freely available in source code (written in C) or already compiled for your machine. All Linux shells can be viewed as highly sophisticated, special-purpose programming languages containing all the usual constructs found in a programming language. The special purpose of Linux shell languages is to tie together the many small commands and utilities found in the Linux environment. By making use of I/O redirection and background processing, the shell languages allow you to write complex programs with minimal effort. For more information, see these chapters:
</P>
<DL>
<DD><B>•</B> Chapter 5, “Running Linux Applications,” for basic information on navigating through Linux.
<DD><B>•</B> Chapter 8, “Using the <TT>vi</TT> Editor,” for information on editing text files.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="376-378.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch19/381-384.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -