14

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?

5 Answers 5

18

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.

10

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.

5

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.

2
  • That caret syntax is actually slower than pressing up arrow and editing the command line yourself...
    – badp
    Oct 21, 2010 at 12:40
  • 3
    !!$ can be written !$ too.
    – Mikel
    Apr 7, 2011 at 23:07
4

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 $_
0

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

1
  • I'm hoping for something faster :)
    – badp
    Oct 21, 2010 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .