📄 ch11_07.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 11] 11.7 History Substitutions </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:36:07Z"><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="ch11_01.htm"TITLE="11. The Lessons of History"><LINKREL="prev"HREF="ch11_06.htm"TITLE="11.6 Using !$ for Safety with Wildcards "><LINKREL="next"HREF="ch11_08.htm"TITLE="11.8 Repeating a Cycle of Commands "></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="ch11_06.htm"TITLE="11.6 Using !$ for Safety with Wildcards "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.6 Using !$ for Safety with Wildcards "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 11<BR>The Lessons of History</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch11_08.htm"TITLE="11.8 Repeating a Cycle of Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 11.8 Repeating a Cycle of Commands "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0237">11.7 History Substitutions </A></H2><PCLASS="para">[Although most of the examples here use <ICLASS="filename">echo</I> to demonstrateclearly just what is going on,you'll normally use history with other UNIX commands. -JP]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12562"></A>The exclamation point (<CODECLASS="literal">!</CODE>) is the C shell's<SPANCLASS="link">default (<ACLASS="linkend"HREF="ch11_15.htm"TITLE="Changing C Shell History Characters with histchars ">11.15</A>)</SPAN>history substitutioncharacter.(It's used in <EMCLASS="emphasis">bash</EM>, too.)This allows you to recall previously entered commands and re-executethem without retyping.The number of commands saved is up to you.To set this number, put a line in your<SPANCLASS="link">shell setup file (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set history=40 <ICLASS="lineannotation">...C shell</I>HISTSIZE=40 <ICLASS="lineannotation">...bash, ksh</I></PRE></BLOCKQUOTE></P><PCLASS="para">This means that the C shell will save the last 40 commands.To list out these 40 commands use:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>history</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12579"></A><ACLASS="indexterm"NAME="AUTOID-12583"></A><ACLASS="indexterm"NAME="AUTOID-12587"></A>To see just the last ten commands in <EMCLASS="emphasis">csh</EM> or <EMCLASS="emphasis">bash</EM>,use <CODECLASS="literal">history 10</CODE>.In the C shell, you can also list out the commands in reverse with<CODECLASS="literal">history -r</CODE>.</P><PCLASS="para">To use the <CODECLASS="literal">!</CODE> in a command line, you have several choices.Some of the following examples are more of a headache than they may beworth.But they are used mostly to<SPANCLASS="link">select arguments from the command line in aliases (<ACLASS="linkend"HREF="ch10_03.htm"TITLE="C Shell Aliases with Command-Line Arguments ">10.3</A>)</SPAN>.Here they are:<ACLASS="indexterm"NAME="UPT-ART-237-IX-EXCLAMATION-POINT-HISTORY-SUBSTITUTION-COMMANDS"></A></P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!!</B></CODE> repeats the last command.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!:</B></CODE> repeats the last command.This form is used if you want to add a <SPANCLASS="link">modifier (<ACLASS="linkend"HREF="ch09_06.htm"TITLE="String Editing (Colon) Operators ">9.6</A>)</SPAN>like: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo xy</B></CODE>xy% <CODECLASS="userinput"><B>!:s/xy/yx</B></CODE>echo yxyx</PRE></BLOCKQUOTE></P><PCLASS="para">The second <CODECLASS="literal">!</CODE> was left out.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!so</B></CODE> repeats the last command that starts with <CODECLASS="literal">so</CODE>.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!?fn?</B></CODE> repeats the last command that has <CODECLASS="literal">fn</CODE> anywhere in it.The string could be found in an argument or in the command name.This is opposed to <CODECLASS="literal">!fn</CODE>, in which <CODECLASS="literal">!fn</CODE> must be in a command name.(The last <CODECLASS="literal">?</CODE> need not be there.Thus <CODECLASS="literal">!?fn</CODE> means the same thing.)</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!34</B></CODE> executes command number 34.You can find the appropriate history number when you list your history usingthe <EMCLASS="emphasis">history</EM> command, or by<SPANCLASS="link"> putting the history number in your prompt . (<ACLASS="linkend"HREF="ch07_02.htm"TITLE="Basics of Setting the Prompt ">7.2</A>)</SPAN></P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!! &</B></CODE> adds an ampersand (<CODECLASS="literal">&</CODE>) to the end of the last command,which executes it and places it into the background.You can add anything to the end of a previous command. For example: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat -v foo</B></CODE> ...% <CODECLASS="userinput"><B>!! | more</B></CODE>cat -v foo | more ...</PRE></BLOCKQUOTE></P><PCLASS="para">In this case the shell will repeat the command to be executed and run it, addingthe pipe through the<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>pager.Another common usage is: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat -v foo</B></CODE> ...% <CODECLASS="userinput"><B>!! > out</B></CODE>cat -v foo > out</PRE></BLOCKQUOTE></P><PCLASS="para">which returns the command but redirects the output into a file.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!:0</B></CODE> selects only the command name; rather than the entire command line. </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>/usr/bin/grep Ah fn1</B></CODE> ...% <CODECLASS="userinput"><B>!:0 Bh fn2</B></CODE>/usr/bin/grep Bh fn2</PRE></BLOCKQUOTE></P><PCLASS="para">Note that as an <SPANCLASS="link">operator (<ACLASS="linkend"HREF="ch09_06.htm"TITLE="String Editing (Colon) Operators ">9.6</A>)</SPAN><CODECLASS="literal">:0</CODE> can be appended to these history substitutions as well.For example, <CODECLASS="literal">!!:0</CODE> will give the last command name, anda colon followed by any number will give the corresponding argument.For example: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat fn fn1 fn2</B></CODE> ...% <CODECLASS="userinput"><B>more !:3</B></CODE>more fn2 ...</PRE></BLOCKQUOTE></P><PCLASS="para">gives the third argument.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!:2-4</B></CODE>gives the second through the fourth argument, or any numbers you choose: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo 1 2 3 4 5</B></CODE>1 2 3 4 5% <CODECLASS="userinput"><B>echo !:2-4</B></CODE>echo 2 3 42 3 4</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!:-3</B></CODE> gives zero through the third argument, or any number you wish: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo 1 2 3 4</B></CODE>1 2 3 4% <CODECLASS="userinput"><B>echo !:-3</B></CODE>echo echo 1 2 3echo 1 2 3</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!^</B></CODE> gives the first argument of the previous command.This is the same as <CODECLASS="literal">!:1</CODE>.Remember that, just as the <CODECLASS="literal">^</CODE> (caret) is the beginning-of-lineanchor in<SPANCLASS="link">regular expressions (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>,<CODECLASS="literal">!^</CODE> gives the beginning history argument. </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat fn fn1 fn2</B></CODE> ...% <CODECLASS="userinput"><B>more !^</B></CODE>more fn ...</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12694"></A><CODECLASS="userinput"><B>!$</B></CODE> gives the last argument of the last command.In the same way that <CODECLASS="literal">$</CODE> (dollar sign) is the end-of-line anchor inregular expressions,<CODECLASS="literal">!$</CODE> gives the ending history argument.Thus: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat fn</B></CODE> ...% <CODECLASS="userinput"><B>more !$</B></CODE>more fn ...</PRE></BLOCKQUOTE></P><PCLASS="para">The new command (<CODECLASS="literal">more</CODE>) is given the last argument of the previouscommand.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!*</B></CODE> is shorthand for the first through the last argument.This is used a lot in aliases: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -