How to use arguments from previous command?

2019-01-12 13:17发布

问题:

I know that Esc + . gives you the last argument of the last command.

But I'm interested in first argument of the last command. Is there a key binding to do so?

On the same lines, is there a generic way of getting the nth argument from the last command? I know that in a bash script, you can use $0, $1 etc., but these don't work on the commandline.

Also, what about iterating through the 0th argument of previous commands, like we can do with the last argument by continuously pressing Esc + .?

回答1:

Just as M-. (meta-dot or esc-dot or alt-dot) is the readline function yank-last-arg, M-C-y (meta-control-y or esc-ctrl-y or ctrl-alt-y) is the readline function yank-nth-arg. Without specifying n, it yanks the first argument of the previous command.

To specify an argument, press Escape and a number or hold Alt and press a number. You can do Alt--to begin specifying a negative number then release Alt and press the digit (this will count from the end of the list of arguments.

Example:

Enter the following command

$ echo a b c d e f g
a b c d e f g

Now at the next prompt, type echo (with a following space), then

Press Alt-Ctrl-y and you'll now see:

$ echo a

without pressing Enter yet, do the following

Press Alt-3 Alt-Ctrl-y

Press Alt-- 2 Alt-Ctrl-y

Now you will see:

$ echo ace

By the way, you could have put the echo on the line by selecting argument 0:

Press Alt-0 Alt-Ctrl-y

Edit:

To answer the question you added to your original:

You can press Alt-0 then repeatedly press Alt-. to step through the previous commands (arg 0). Similarly Alt-- then repeating Alt-. would allow you to step through the previous next-to-last arguments.

If there is no appropriate argument on a particular line in history, the bell will be rung.

If there is a particular combination you use frequently, you can define a macro so one keystroke will perform it. This example will recall the second argument from previous commands by pressing Alt-Shift-Y. You could choose any available keystroke you prefer instead of this one. You can press it repeatedly to step through previous ones.

To try it out, enter the macro at a Bash prompt:

bind '"\eY": "\e2\e."'

To make it persistent, add this line to your ~/.inputrc file:

"\eY": "\e2\e."

Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers.



回答2:

!$ gets the last element of the previous command line argument.



回答3:

To use the first argument, you can use !^ or !:1

Example:

$ echo a b c d e 
a b c d e
$ echo !^
echo a
a

$ echo a b c d e 
a b c d e
$ echo !:1
echo a
a

Since your question is about using any other arguments, here are some useful ones:

!^      first argument
!$      last argument
!*      all arguments
!:2     second argument

!:2-3   second to third arguments
!:2-$   second to last arguments
!:2*    second to last arguments
!:2-    second to next to last arguments

!:0     the command
!!      repeat the previous line

The first four forms are more often used. The form !:2- is somewhat counter-intuitive, as it doesn't include the last argument.



回答4:

I liked @larsmans answer so much I had to learn more. Adding this answer to help others find the man page section and know what to google for:

$ man  -P 'less -p ^HISTORY\ EXPANSION' bash
<...>
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 command word.
   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 synonym for ‘1-$’.  
          It is not an error to use * if there is just one word 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, the previous command is used as the event.


回答5:

!^ may be the command for the first argument. i'm not sure if there is a way to get the nth.



回答6:

You can also get arguments from any command in your history!


$ echo a b c d e f g
a b c d e f g
$ echo build/libs/jenkins-utils-all-0.1.jar
build/libs/jenkins-utils-all-0.1.jar
$ history | tail -5
  601  echo build/libs/jenkins-utils-all-0.1.jar
  602  history | tail -10
  603  echo a b c d e f g
  604  echo build/libs/jenkins-utils-all-0.1.jar
  605  history | tail -5
$ echo !-3:4
echo d
d
$ echo !604:1
echo build/libs/jenkins-utils-all-0.1.jar
build/libs/jenkins-utils-all-0.1.jar



回答7:

Basically it has a use in yanking previous (command's) arguments.

For instance, if the following command is issued:

echo Hello, world how are you today?

then, Hello, will be the first argument, and today? the sixth, that is the last one; meaning it can be referenced by typing:

Alt+6 followed by Ctrl-Alt-6


Ctrl is traditionally denoted as a hat character ^ prepended to keys names, and Alt as M- that is Meta prefix.

So the above shortcut can be redefined as ^My to yank.


Also, there is hats substitution shortcut in the command line:

echo Hello, world!

^Hello^Bye

Bye, world!

to substitute the previous command's first matched string, meaning:

Hello, world! Hello, people!

^Hello^Bye

would result in:

Bye, world! Hello, people!

leaving the second match (hello) unchanged.

Note: Do not leave space between hats, or the operation won't work.


The above is just a shortcut for:

!:s/Hello/Bye

event-level(*) substitution for the first found (matched) string in the previous command, while prefixing the first part with the g switch will apply to the whole line globally:

echo Hello, world! Hello, people!

!:gs/Hello/Bye

Bye, world! Bye, people!

as usually being done in other related commands such as sed, vi, and in regex (regular expression) - a standart way to search (match string).

No, you can't do !:sg/Hello/Bye or !:s/Hello/Bye/g here, that's the syntax!


  • ! is for events; event might be understood as command output or operation done in the commands history.

That's what I understood by using it myself and trying things on my own from what I read from various sources including manual pages, blogs, and forums.

Hope it will shed some light into mysterious ways of bash, the Bourne-Again shell (a play on sh shell, which itself is called Bourne shell after its inventor's last name), what is default shell in many distributions including servers (server OS's).



回答8:

The method described at the end of the accepted answer also works with the zeroth argument for me. I have these lines in my ~/.inputrc:

"\en": "\e0\e."
"\em": "\e1\e."
"\e,": "\e2\e."

\e2\e. has the advantage over \e2\e\C-y that it cycles through previous commands if it is pressed repeatedly instead of inserting the second argument of the previous command multiple times.

To insert the whole previous command, you can type !!\e^. \e^ is history-expand-line.



回答9:

!^ will get you the first param, !$ will get you the last param, !:n will get you the nth element.