Reusing Arguments

Problem

Reusing the last command was easy with !! but you might not want the whole command. How can you reuse just the last argument?

Solution

Use !$ to indicate the last command. Use !:1 for the first argument on the command line, !:2 for the second, and so on.

Discussion

It is quite common to hand the same filename to a series of commands. One of the most common occurrences might be the way a programmer would edit and then compile, edit and then compile.… Here, the !$ comes in quite handy:

$ vi /some/long/path/name/you/only/type/once
...
$ gcc !$
gcc /some/long/path/name/you/only/type/once
...
$ vi !$
vi /some/long/path/name/you/only/type/once
...
$ gcc !$
gcc /some/long/path/name/you/only/type/once

Get the idea? It saves a lot of typing but it also avoids errors. If you mistype the filename when you compile, then you are not compiling the file that you just edited. With !$ you always get the name of the file on which you just worked. If the argument you want is buried in the middle of the command line, you can get at it with the numbered “bang-colon” commands. Here’s an example:

$ munge /opt/my/long/path/toa/file | more
...
$ vi !:1
vi /opt/my/long/path/toa/file

You might be tempted to try to use !$, but in this instance it would yield more, which is not the name of the file that you want to edit.

See Also

  • The bash manpage to read about “Word Designators”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.60