Be Playful in Bash

HISTORY EXPANSION

Event Designators

An event designator is a reference to a command line entry in thehistory list.

!
Start a history substitution, except when followed by ablank,newline, carriage return, =or ( (when the extglob shell option is enabled usingthe shopt builtin).
!n
Refer to command linen.
!-n
Refer to the current command line minusn.
!!
Refer to the previous command. This is a synonym for `!-1'.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containingstring.The trailing ? may be omitted ifstringis followed immediately by a newline.
^string1^string2^
Quick substitution. Repeat the last command, replacingstring1withstring2.Equivalent to``!!:s/string1/string2/''(see Modifiers below).
!#
The entire command line typed so far.

Word Designators

Word designators are used to select desired words from the event.A :separates the event specification from the word designator.It may be omitted if the word designator begins with a^,$,*,-,or%.Words are numbered from the beginning of the line,with the first word being denoted by 0 (zero).Words are inserted into the current line separated by single spaces.

0 (zero)
The zeroth word. For the shell, this is the commandword.
n
The nth word.
^
The first argument. That is, word 1.
$
The last argument.
%
The word matched by the most recent `?string?' search.
x-y
A range of words; `-y' abbreviates `0-y'.
*
All of the words but the zeroth. This is a synonymfor `1-$'. It is not an error to use*if there is just oneword in the event; the empty string is returned in that case.
x*
Abbreviates x-$.
x-
Abbreviates x-$ like x*, but omits the last word.

If a word designator is supplied without an event specification, theprevious command is used as the event. 

 

Modifiers

After the optional word designator, there may appear a sequence ofone or more of the following modifiers, each preceded by a `:'.

 

h
Remove a trailing file name component, leaving only the head.
t
Remove all leading file name components, leaving the tail.
r
Remove a trailing suffix of the form .xxx, leaving thebasename.
e
Remove all but the trailing suffix.
p
Print the new command but do not execute it.
q
Quote the substituted words, escaping further substitutions.
x
Quote the substituted words as withq,but break into words atblanksand newlines.
s/old/new/
Substitutenewfor the first occurrence ofoldin the event line. Any delimiter can be used in place of /. Thefinal delimiter is optional if it is the last character of theevent line. The delimiter may be quoted inoldandnewwith a single backslash. If & appears innew,it is replaced byold.A single backslash will quote the &. Ifoldis null, it is set to the lastoldsubstituted, or, if no previous history substitutions took place,the laststringin a!?string[?]search.
&
Repeat the previous substitution.
g
Cause changes to be applied over the entire event line. This isused in conjunction with `:s' (e.g., `:gs/old/new/')or `:&'. If used with`:s', any delimiter can be usedin place of /, and the final delimiter is optionalif it is the last character of the event line.An a may be used as a synonym for g.
G
Apply the following `s' modifier once to each word in the event line.

posted on 2010-09-13 15:25  David Young 杨博华  阅读(318)  评论(0编辑  收藏  举报

导航