Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Often times I issue different commands on the same file. For example:

$ youtube-dl aB54c4p0eo #I made this video id up on the spot
$ mv aB54c4p0eo.flv kittens.flv
$ vlc kittens.flv
$ rm kittens.flv

Is there a way to reuse arguments from the previous command in the current so that I don't have to rewrite it?

share|improve this question

5 Answers 5

up vote 9 down vote accepted

alt-. is certainly nice, but if you happen to already know which numbered argument you want, you can be faster: !:n is the nth argument of the previous command.

It's often helpful to combine this with magic space. To enable that, put in your .inputrc Space: magic-space. With that enabled, when you type space after !:2, it will be immediately expanded to its value instead of waiting for you to hit enter. Saves you from accidentally grabbing the wrong argument.

share|improve this answer

In bash you can use the shortcut Alt + ..
Hitting it once, will give you the last argument. Hitting it more will cycle through your last arguments.

share|improve this answer

In bash, you can also use $_ for the last command line argument of the last command you typed:

$ youtube-dl aB54c4p0eo #I made this video id up on the spot
$ mv aB54c4p0eo.flv kittens.flv
$ vlc kittens.flv
$ rm kittens.flv

becomes:

$ youtube-dl aB54c4p0eo #I made this video id up on the spot
$ mv $_ kittens.flv
$ vlc $_
$ rm $_
share|improve this answer

In bash, the designator for the "last word on previous command line" is !!$ :

$ echo hello world
hello world
$ echo goodbye !!$
echo goodbye world # this is bash echoing actual cmd line before execution
goodbye world

You can also use the "caret syntax" to replace the initial part of the command line; this comes handy if you want to execute several commands on the same file:

file file.dat
^file^ls -l^ #gives `ls -l file.dat`
^ls -l^stat # gives `stat file.dat`

There are many more possibilities; see "History substitution" in the bash(1) man page for details.

share|improve this answer
    
That caret syntax is actually slower than pressing up arrow and editing the command line yourself... –  badp Oct 21 '10 at 12:40
    
!!$ can be written !$ too. –  Mikel Apr 7 '11 at 23:07

One relatively slow way is recalling the previous command with and replacing the previous command with the newer one.

share|improve this answer
    
I'm hoping for something faster :) –  badp Oct 21 '10 at 11:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.