Bash History Cheat Sheet Page 2

ADVERTISEMENT

History expansion:
Shortcut
Description
Event Designators:
!
Starts a history substitution.
!!
Refers to the last command.
!n
Refers to the n-th command line.
!-n
Refers to the current command line minus n.
!string
Refers to the most recent command starting with string.
!?string?
Refers to the most recent command containing string (the ending ? is optional).
ˆstring1ˆstring2ˆ
Quick substitution. Repeats the last command, replacing string1 with string2.
!#
Refers to the entire command line typed so far.
Word Designators (word designators follow the event designators, separated by a colon):
0
The zeroth (first) word in a line (usually command name).
n
The n-th word in a line.
ˆ
The first argument (the second word) in a line.
$
The last argument in a line.
%
The word matched by the most recent ?string? search.
x-y
A range of words from x to y (-y is synonymous with 0-y).
*
All word but the zeroth.
x*
Synonymous with x-$.
x-
The words from x to the second to last word.
Modifiers (modifiers follow word designators, separated by a colon):
h
Removes a trailing pathname component, leaving the head.
t
Removes all leading pathname components, leaving the tail.
r
Removes a trailing suffix of the form .xxx, leaving the basename.
e
Removes all but the trailing suffix.
p
Prints the resulting command but does not execute it.
q
Quotes the substituted words, escaping further substitutions.
x
Quotes the substituted words, breaking them into words at blanks and newlines.
s/old/new/
Substitutes new for old.
&
Repeats the previous substitution.
g
Causes s/old/new/ or & to be applied over the entire event line.
History expansion examples:
$ echo a b c d e
(executes ‘echo ab c d e‘)
$ tar -xzf package-x.y.z.tgz
a b c d e
...
$ echo !!:3-$
(executes ‘echo c d e‘)
$ cd !-1:$:r
(exec’s ‘cd package-x.y.z‘)
c d e
package-x.y.z $
$ echo !-2:*:q
(executes ‘echo ’a b c d e’‘)
a b c d e
$ ls -a /tmp
$ echo !-3:1:2:4:x
(executes ‘echo ’a’ ’b’ ’d’‘)
file1
file2
file3 ...
a b d
$ ^-a^-l^
(exec’s ‘ls -l /tmp‘)
$ echo !-4:1-3:s/a/foo/:s/b/bar/:s/c/baz/
-rw-------
1
user
user
file1
(executes ‘echo foo bar baz‘)
...
foo bar baz
A cheat sheet by Peteris Krumins (peter@catonmat.net), 2008.
- good coders code, great reuse
Released under GNU Free Document License.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2