218-220.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 177 行
HTML
177 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:bash</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=11//-->
<!--PAGES=218-220//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="215-218.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="220-224.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Prompts</FONT></H4>
<P><TT>bash</TT> has two levels of user prompts. The first level is what you see when <TT>bash</TT> is waiting for a command to be typed. (This is what you normally see when you are working with <TT>bash</TT>.)</P>
<P>The default first-level prompt is the <TT>%</TT> character. If you do not like the <TT>%</TT> character as the prompt or prefer to customize your prompt, you can do so by setting the value of the <TT>PS1 bash</TT> variable. For example:</P>
<!-- CODE SNIP //-->
<PRE>
PS1=“Please enter a command”
</PRE>
<!-- END CODE SNIP //-->
<P>sets the <TT>bash</TT> shell prompt to the specified string.</P>
<P>The second level of prompt is displayed when <TT>bash</TT> is expecting more input from you in order to complete a command. The default for the second-level prompt is <TT>></TT>. If you want to change the second-level prompt, you can set the value of the <TT>PS2</TT> variable, as in:</P>
<!-- CODE SNIP //-->
<PRE>
PS2=“I need more information”
</PRE>
<!-- END CODE SNIP //-->
<P>In addition to displaying static character strings in the command prompts (as in the two preceding examples), you can also use some predefined special characters. These special characters place things such as the current time into the prompt. Table 11.1 lists the most commonly used special character codes.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 11.1.</B> Prompt special character codes.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Character
<TH WIDTH="70%" ALIGN="LEFT">Meaning
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>\!</TT>
<TD>Displays the history number of this command.
<TR>
<TD><TT>\#</TT>
<TD>Displays the command number of the current command.
<TR>
<TD VALIGN="TOP"><TT>\$</TT>
<TD>Displays a $ in the prompt unless the user is root. When the user is root, it displays a #.
<TR>
<TD><TT>\\</TT>
<TD>Displays a backslash.
<TR>
<TD><TT>\d</TT>
<TD>Displays the current date.
<TR>
<TD VALIGN="TOP"><TT>\h</TT>
<TD>Displays the host name of the computer on which the shell is running.
<TR>
<TD VALIGN="TOP"><TT>\n</TT>
<TD>Prints a newline character. This causes the prompt to span more than one line.
<TR>
<TD VALIGN="TOP"><TT>\nnn</TT>
<TD>Displays the character that corresponds to the octal value of the number nnn.
<TR>
<TD><TT>\s</TT>
<TD>The name of the shell that is running.
<TR>
<TD><TT>\t</TT>
<TD>Displays the current time.
<TR>
<TD><TT>\u</TT>
<TD>Displays the username of the current user.
<TR>
<TD><TT>\W</TT>
<TD>Displays the base name of the current working directory.
<TR>
<TD><TT>\w</TT>
<TD>Displays the current working directory.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>These special characters can be combined into several useful prompts to provide you with information about where you are. (They can be combined in very grotesque ways, too!) Several examples of setting the <TT>PS1</TT> prompt follow here:</P>
<!-- CODE SNIP //-->
<PRE>
PS1=“\t”
</PRE>
<!-- END CODE SNIP //-->
<P>This causes the prompt to have the following appearance (there is no space after the prompt):
</P>
<!-- CODE SNIP //-->
<PRE>
02:16:15
</PRE>
<!-- END CODE SNIP //-->
<P>The prompt string
</P>
<!-- CODE SNIP //-->
<PRE>
PS1=\t
</PRE>
<!-- END CODE SNIP //-->
<P>causes the prompt to have the following appearance:
</P>
<!-- CODE SNIP //-->
<PRE>
t
</PRE>
<!-- END CODE SNIP //-->
<P>This shows the importance of including the character sequence in quotation marks. The prompt string
</P>
<!-- CODE SNIP //-->
<PRE>
PS1=“\t\\ ”
</PRE>
<!-- END CODE SNIP //-->
<P>causes the prompt to look like this:
</P>
<!-- CODE SNIP //-->
<PRE>
02:16:30\
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, there is a space following the prompt because there was a space within the quotation marks.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Job Control</FONT></H4>
<P><I>Job control</I> refers to the ability to control the execution behavior of a currently running process. Specifically, you can suspend a running process and cause it to resume running later. <TT>bash</TT> keeps track of all processes that it starts (as a result of user input) and lets you suspend a running process or restart a suspended one at any time during the life of that process. (For more information on processes, see Chapter 34, “Processes.”)</P>
<P>Pressing Ctrl+Z suspends a running process. The <TT>bg</TT> command restarts a suspended process in the background, whereas the <TT>fg</TT> command restarts a process in the foreground.</P>
<P>These commands are most often used when a user wants to run a command in the background but accidentally starts it in the foreground. When a command is started in the foreground, it locks the shell from any further user interaction until the command completes execution. This is usually not a problem because most commands take only a few seconds to execute. If the command you are running takes a long time, though, start the command in the background so that you can continue to use <TT>bash</TT> to enter other commands in the foreground.</P>
<P>For example, if you start the command <TT>find / -name “test” > find.out</TT> (which scans the entire file system for files named <TT>test</TT> and stores the results in a file called <TT>find.out</TT>) in the foreground, your shell may be tied up for many seconds or even minutes, depending on the size of the file system and the number of users on the system. If you issue this command and want to continue executing in the background so you can use the system again, enter the following:</P>
<!-- CODE SNIP //-->
<PRE>
control-z
bg
</PRE>
<!-- END CODE SNIP //-->
<P>This first suspends the <TT>find</TT> command, then restarts it in the background. The <TT>find</TT> command continues to execute, and you have <TT>bash</TT> back again.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="215-218.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="220-224.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?