rhl10.htm
来自「linux的初学电子书」· HTM 代码 · 共 1,898 行 · 第 1/4 页
HTM
1,898 行
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl10.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl11.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl11.htm">
<LINK REL="Previous" HREF="rhl09.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl09.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl10.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl09.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl09.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl11.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl11.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E63" >Shells in a Nutshell</A>
<UL>
<LI>
<A HREF="#E69E163" >What Is a Shell?</A>
<LI>
<A HREF="#E69E164" >How the Shell Gets Started</A>
<LI>
<A HREF="#E69E165" >The Most Common Shells</A></UL>
<LI>
<A HREF="#E68E64" >The Bourne Again Shell</A>
<UL>
<LI>
<A HREF="#E69E166" >Command-Line Completion</A>
<LI>
<A HREF="#E69E167" >Wildcards</A>
<LI>
<A HREF="#E69E168" >Command History</A>
<LI>
<A HREF="#E69E169" >Aliases</A>
<LI>
<A HREF="#E69E170" >Input Redirection</A>
<LI>
<A HREF="#E69E171" >Output Redirection</A>
<LI>
<A HREF="#E69E172" >Pipelines</A>
<LI>
<A HREF="#E69E173" >Prompts</A>
<LI>
<A HREF="#E69E174" >Job Control</A></UL>
<LI>
<A HREF="#E68E65" >Customizing bash</A>
<LI>
<A HREF="#E68E66" >bash Command Summary</A>
<LI>
<A HREF="#E68E67" >bash Variables</A>
<LI>
<A HREF="#E68E68" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E10"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>10</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E10"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Using </B><B>bash</B></FONT></CENTER></H2>
<BR>
<P>This chapter looks at the shells in a little more detail. You'll start with bash (Bourne Again Shell), the default shell used by Linux and the most popular shell for new users. In this chapter you will learn
<BR>
<UL>
<LI>What a shell is
<BR>
<BR>
<LI>The most common shells used in Linux
<BR>
<BR>
<LI>Command-line completion and wildcards
<BR>
<BR>
<LI>Command history and aliases
<BR>
<BR>
<LI>Redirection and pipes
<BR>
<BR>
<LI>Changing prompts
<BR>
<BR>
<LI>Job control
<BR>
<BR>
<LI>How to customize your bash shell
<BR>
<BR>
</UL>
<P>You will also look at the most commonly used bash commands and the environment variables bash uses. By the end of this chapter, you should be able to work faster and more efficiently with bash.
<BR>
<BR>
<A NAME="E68E63"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Shells in a Nutshell</B></FONT></CENTER></H3>
<BR>
<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.
<BR>
<BR>
<A NAME="E69E163"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>What Is a Shell?</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<P><B> <A HREF="10rhl01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/10rhl01.gif">Figure 10.1. The relationship between the user </B><B>and the shell.</A></B>
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>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).</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<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.
<BR>
<P>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.
<BR>
<P>Some of the commands, such as the print working directory (pwd) command, are built into the Linux bash shell. Other commands, such as the copy command (cp) and the remove command (rm), are separate executable programs that exist in one of the
directories in the filesystem. As the user, you don't know (or probably 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>
<P><B> <A HREF="10rhl02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/10rhl02.gif">Figure 10.2. Command interpretation by the </B><B>shell.</A></B>
<BR>
<P>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 cd or pw. 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 ls and rm, or they can be application programs that are either purchased commercially, such as xv, or available as public domain software, such as
ghostview.
<BR>
<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.
<BR>
<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.
<BR>
<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.
<BR>
<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 a executable shell program. This means that the shell language can be
used to simplify repetitive tasks. See <A HREF="rhl13.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl13.htm">Chapter 13</A>, "Shell Programming," for more information on shell programming.
<BR>
<BR>
<A NAME="E69E164"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>How the Shell Gets Started</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<P>Each user on your system has a default shell. The default shell for each user is specified in the system password file, called /etc/passwd. 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.
<BR>
<BR>
<A NAME="E69E165"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The Most Common Shells</B></FONT></CENTER></H4>
<BR>
<P>Several different kinds of shells are available on Linux and UNIX systems. The most common are the Bourne shell (called sh), the C shell (csh), and the Korn shell (ksh). Each of these three shells has its own advantages and disadvantages.
<BR>
<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.
<BR>
<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.
<BR>
<P>The Korn shell (ksh) 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. ksh is efficient and has both a good interactive interface
and a good programming interface.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>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.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<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 tcsh (an extension of csh), the
Bourne Again Shell (bash, an extension of sh), and the Public Domain Korn Shell (pdksh, an extension of ksh). bash is the default shell on most Linux systems.
<BR>
<BR>
<A NAME="E68E64"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Bourne Again Shell</B></FONT></CENTER></H3>
<BR>
<P>The Bourne Again Shell (bash), as its name implies, is an extension of the Bourne shell. bash is fully backward-compatible with the Bourne shell, but contains many enhancements and extra features that are not present in the Bourne shell. bash also
contains many of the best features that exist in the C and Korn shells. bash has a very flexible and powerful programming interface, as well as a user-friendly command interface.
<BR>
<P>Why use bash instead of sh? The biggest drawback of the Bourne shell is the way that it handles user input. Typing commands into the Bourne shell can often be very tedious, especially if you are using it on a regular basis and typing in a large number
of commands. bash provides several features that make entering commands much easier.
<BR>
<BR>
<A NAME="E69E166"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Command-Line Completion</B></FONT></CENTER></H4>
<BR>
<P>Often when you enter commands into bash (or any other shell), the complete text of the command is not necessary in order for the shell to be able to determine what you want it to do. For example, assume that the current working directory contains the
following files and subdirectories:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">News/ bin/ games/ mail/ samplefile test/</FONT></PRE>
<P>If you want to change directories from the current working directory to the test subdirectory, you would enter the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd test</FONT></PRE>
<P>Although this command will work, bash enables you to accomplish the same thing in a slightly different way. Since test is the only file in the directory that begins with the letter t, bash should be able to figure out what you want to do after you type
in the letter t alone:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd t</FONT></PRE>
<P>After the letter has been typed, the only thing that you could be referring to is the test subdirectory. To get bash to finish the command for you, press the Tab key:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd t<tab></FONT></PRE>
<P>When you do this, bash finishes the command for you and displays it on the screen. The command doesn't actually execute until you press the Enter key to verify that the command bash came up with is the command that you really intended.
<BR>
<P>For short commands like this, you might not see very much value in making use of command-line completion. Using this feature may even slow you down when typing short commands. After you get used to using command-line completion, though, and when the
commands that you are entering get a little longer, you will wonder how anyone lived without this feature.
<BR>
<P>So what happens if more than one file in the directory begins with the letter t? It would seem that this would cause a problem if you wanted to use command-line completion. Let's see what happens when you have the following directory contents:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">News/ bin/ mail/ samplefile test/ tools/ working/</FONT></PRE>
<P>Now you have two files in the directory that start with the letter t. Assuming that you still want to cd into the test subdirectory, how do you do it using command-line completion? If you type cd t<tab> as you did before, bash will not know which
subdirectory you want to change to because the information you have given is not unique.
<BR>
<P>If you try to do this, bash will beep to notify you that it does not have enough information to complete the command. After beeping, bash will leave the command on the screen as it was entered. This enables you to enter more information without retyping
what was already typed. In this case, you only need to enter an e and press the Tab key again. This will give bash enough information to complete the command on the command line for you to verify:
<BR>
<BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?