Word Designators
Word designators are used to select desired words from the event. They are optional; if the word designator isn’t supplied, the history expansion uses the entire 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). That first word is usually the command word, and the arguments begin with the second word. Words are inserted into the current line separated by single spaces.
For example,
!!-
designates the preceding command. When you type this, the preceding command is repeated in toto.
!!:$-
designates the last word of the preceding command. This may be shortened to
!$. !fi:2designates the second argument of the most recent command starting with the letters
fi.
Here are the word designators:
0 (zero)-
The
0th word. For the shell, and many other, applications, this is the command word. n-
The nth word.
^-
The first argument: word 1.
$-
The last word. This is usually the last argument, but expands to the zeroth word if there is only one word in the line.
%-
The first word matched by the most recent ‘?string?’ search, if the search string begins with a character that is part of a word. By default, searches begin at the end of each line and proceed to the beginning, so the first word matched is the one closest to the end of the line.
x-y-
A range of words; ‘-y’ abbreviates ‘0-y’.
*-
All of the words, except the
0th. This is a synonym for ‘1-$’. It is not an error to use ‘*’ if there is just one word in the event; it expands to the empty string in that case. x*-
Abbreviates ‘x-$’.
x--
Abbreviates ‘x-$’ like ‘x*’, but omits the last word. If ‘x’ is missing, it defaults to 0.
If a word designator is supplied without an event specification, the previous command is used as the event, equivalent to !!.
Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.
Licensed under the GNU Free Documentation License.
https://www.gnu.org/software/bash/manual/html_node/Word-Designators.html