📄 ch10.htm
字号:
<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>
-->
<H1></H1>
<UL>
<LI><A HREF="#Heading1">- 10 -</A>
<UL>
<LI><A HREF="#Heading2">bash</A>
<UL>
<LI><A HREF="#Heading3">Shells in a Nutshell</A>
<UL>
<LI><A HREF="#Heading4">What Is a Shell?</A>
</UL>
<LI><A HREF="#Heading5">NOTE</A>
<UL>
<LI><A HREF="#Heading6">FIGURE 10.1.</A>
<LI><A HREF="#Heading7">FIGURE 10.2.</A>
</UL>
<LI><A HREF="#Heading8">How the Shell Gets Started</A>
<UL>
<LI><A HREF="#Heading9">The Most Common Shells</A>
</UL>
<LI><A HREF="#Heading10">NOTE</A>
<LI><A HREF="#Heading11">The Bourne Again Shell</A>
<UL>
<LI><A HREF="#Heading12">Command-Line Completion</A>
<LI><A HREF="#Heading13">Wildcards</A>
</UL>
<LI><A HREF="#Heading14">NOTE</A>
<UL>
<LI><A HREF="#Heading15">Command History</A>
</UL>
<LI><A HREF="#Heading16">NOTE</A>
<LI><A HREF="#Heading17">NOTE</A>
<UL>
<LI><A HREF="#Heading18">Aliases</A>
</UL>
<LI><A HREF="#Heading19">NOTE</A>
<UL>
<LI><A HREF="#Heading20">Input Redirection</A>
<LI><A HREF="#Heading21">Output Redirection</A>
</UL>
<LI><A HREF="#Heading22">NOTE</A>
<UL>
<LI><A HREF="#Heading23">Pipelines</A>
<LI><A HREF="#Heading24">Prompts</A>
<LI><A HREF="#Heading25">Job Control</A>
</UL>
<LI><A HREF="#Heading26">Customizing bash</A>
<LI><A HREF="#Heading27">NOTE</A>
<LI><A HREF="#Heading28">bash Command Summary</A>
<LI><A HREF="#Heading29">bash Variables</A>
<LI><A HREF="#Heading30">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 10 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">bash</FONT></H2>
<P>IN THIS CHAPTER</P>
<UL>
<LI>Shells in a Nutshell
<P>
<LI>The Bourne Again Shell
<P>
<LI>Customizing bash
<P>
<LI>bash Command Summary
<P>
<LI>bash Variables
</UL>
<P><BR>
This chapter looks at the shells in a little more detail. You'll start with <TT>bash</TT>
(Bourne Again Shell), the default shell used by Linux and the most popular shell
for new users. In this chapter you will learn
<UL>
<LI>What a shell is
<P>
<LI>The most common shells used in Linux
<P>
<LI>Command-line completion and wildcards
<P>
<LI>Command history and aliases
<P>
<LI>Redirection and pipes
<P>
<LI>Changing prompts
<P>
<LI>Job control
<P>
<LI>How to customize your <TT>bash</TT> shell
</UL>
<P>You will also look at the most commonly used <TT>bash</TT> commands and the environment
variables <TT>bash</TT> uses. By the end of this chapter, you should be able to work
faster and more efficiently with <TT>bash</TT>.
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">Shells in a Nutshell</FONT></H3>
<P>What is a shell, anyway? It seems to be a word used all the time in Linux, but
the exact meaning is vague for many new users (and some veterans). This section explains
exactly what a shell program is and why it is so important when using Linux.
<H4 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">What Is a Shell?</FONT></H4>
<P>The shell is a program used to interface between you (the user) and Linux (or,
more accurately, between you and the Linux kernel). Figure 10.1 illustrates the relationship
between the user, the shell, and the Linux kernel. Every command you type at a prompt
on your screen is interpreted by the shell, then passed to the Linux kernel.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading5<FONT COLOR="#000077"><B>NOTE:</B> </FONT>If you are familiar
with MS-DOS, you will recognize this relationship as almost identical to the relationship
between a DOS user and the COMMAND.COM program. The only real difference is that
in the DOS world, no distinction is made between the COMMAND.COM program and DOS
(or to be more accurate, the DOS kernel).
<HR>
</DL>
<P>The shell is a command-language interpreter. It has its own set of built-in shell
commands. The shell can also make use of all of the Linux utilities and application
programs that are available on the system.
<H6></H6>
<P><A NAME="Heading6<FONT COLOR="#000077">
<a href="../art/10/10fig01.gif">FIGURE 10.1. </a></FONT><I>The relationship
between the user and the shell. </I><BR>
<BR>
Whenever you enter a command it is interpreted by the Linux shell. For example, in
earlier chapters when you were introduced to the Linux file- and directory-manipulation
commands, all of the sample commands entered at the command prompt were interpreted
by whichever Linux shell you were using.</P>
<P>Some of the commands, such as the print working directory (<TT>pwd</TT>) command,
are built into the Linux <TT>bash</TT> shell. Other commands, such as the copy command
(<TT>cp</TT>) and the remove command (<TT>rm</TT>), are separate executable programs
that exist in one of the directories in the file system. As the user, you don't know
(or probably don't care) if the command is built into the shell or is a separate
program. Figure 10.2 shows how the shell performs this command interpretation.<BR>
<BR>
<A NAME="Heading7<FONT COLOR="#000077">
<a href="../art/10/10fig02.gif">FIGURE 10.2.</a><I> </I></FONT><I>Command
interpretation by the shell. </I><BR>
<BR>
Figure 10.2 illustrates the steps that the shell takes to figure out what to do with
user commands. It first checks to see if the command is one of its own built-in commands
(like <TT>cd</TT> or <TT>pwd</TT>). If the command is not one of these, the shell
checks to see if it is an application program. Application programs can be utility
programs that are part of Linux, such as <TT>ls</TT> and <TT>rm</TT>, or they can
be application programs that are either purchased commercially, such as <TT>xv</TT>,
or available as public domain software, such as <TT>ghostview</TT>.</P>
<P>The shell tries to find these application programs by looking in all of the directories
that are in your search path. The path is a list of directories where executable
programs can be found. If the command that was entered is not an internal shell command
and it is not an executable file in your path, an error message will be displayed.</P>
<P>As the last step in a successful command, the shell's internal commands and all
of the application programs are eventually broken down into system calls and passed
to the Linux kernel.</P>
<P>Another important aspect of the shell is that it contains a very powerful interpretive
programming language. This language is similar in function to the MS-DOS interpreted
language, but is much more powerful. The shell programming language supports most
of the programming constructs found in high-level languages, such as looping, functions,
variables, and arrays.</P>
<P>The shell programming language is easy to learn, and once known it becomes a very
powerful programming tool. Any command that can be typed at the command prompt can
also be put into an executable shell program. This means that the shell language
can be used to simplify repetitive tasks. See Chapter 13, "Shell Programming,"
for more information on shell programming.
<H4 ALIGN="CENTER"><A NAME="Heading8<FONT COLOR="#000077">How the Shell Gets
Started</FONT></H4>
<P>Earlier in this chapter you learned that the shell is the main method by which
a user interacts with the Linux kernel. But how does this program get initialized
to do so? The shell is started after you successfully log into the system, and it
continues to be the main method of interaction between the user and the kernel until
you log out.</P>
<P>Each user on your system has a default shell. The default shell for each user
is specified in the system password file, called <TT>/etc/passwd</TT>. The system
password file contains, among other things, each person's user ID, an encrypted copy
of each user's password, and the name of the program to run immediately after a user
logs into the system. The program specified in the password file does not have to
be one of the Linux shells, but it almost always is.
<H4 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">The Most Common Shells</FONT></H4>
<P>Several different kinds of shells are available on Linux and UNIX systems. The
most common are the Bourne shell (called <TT>sh</TT>), the C shell (<TT>csh</TT>),
and the Korn shell (<TT>ksh</TT>). Each of these three shells has its own advantages
and disadvantages.</P>
<P>The Bourne shell was written by Steven Bourne. It is the original UNIX shell and
is available on every UNIX system in existence. The Bourne shell is considered to
be very good for UNIX shell programming, but it does not handle user interaction
as well as some of the other shells available.</P>
<P>The C shell, written by Bill Joy, is much more responsive to user interaction.
It supports features such as command-line completion that are not in the Bourne shell.
The C shell's programming interface is thought by many not to be as good as that
of the Bourne shell, but it is used by many C programmers because the syntax of its
programming language is similar to that of the C language. This is also why it is
named the C shell.</P>
<P>The Korn shell (<TT>ksh</TT>) was written by Dave Korn. He took the best features
of both the C shell and the Bourne shell and combined them into one that is completely
compatible with the Bourne shell. <TT>ksh</TT> is efficient and has both a good interactive
interface and a good programming interface.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading10<FONT COLOR="#000077"><B>NOTE:</B> </FONT>There are many
quality reference books about the Bourne, C, and Korn shells. If you want to use
these shells instead of the three shells discussed in this and the next two chapters,
you may want to find a good reference book on the particular shell you prefer. Because
the shells included with Linux are used by most people, we will concentrate on those.
<HR>
</DL>
<P>In addition to these shells, many other shell programs took the basic features
from one or more of the existing shells and combined them into a new version. The
three newer shells that will be discussed in this book are <TT>tcsh</TT> (an extension
of <TT>csh</TT>), the Bourne Again Shell (<TT>bash</TT>, an extension of <TT>sh</TT>),
and the Public Domain Korn Shell (<TT>pdksh</TT>, an extension of <TT>ksh</TT>).
<TT>bash</TT> is the default shell on most Linux systems.
<H3 ALIGN="CENTER"><A NAME="Heading11<FONT COLOR="#000077">The Bourne Again
Shell</FONT></H3>
<P>The Bourne Again Shell (<TT>bash</TT>), as its name implies, is an extension of
the Bourne shell. <TT>bash</TT> is fully backward-compatible with the Bourne shell,
but contains many enhancements and extra features that are not present in the Bourne
shell. <TT>bash</TT> also contains many of the best features that exist in the C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -