bash: reuse last argument from previous command

Reuse the last argument of the previous command with !$:

$ echo abc def
abc def
$ echo !$
def

A common use case would be mkdir and cd:

$ mkdir foo
$ cd !$

You can also insert the last argument of the previous command and continue typing with <ESC>.:

$ echo abc def
abc def
$ echo <ESC>. ghi
def ghi

Oh, the little things… 🙂

2 thoughts on “bash: reuse last argument from previous command

  1. Ford Prefect

    One even less known nifty thing is that the last argument is also available in the bash command prompt using Alt+’.’. This is nifty in cases where you want to re-use the argument, but slightly edit it. Also, you can cycle through the history of last arguments by repeatedly pressing Alt+’.’ — something that !$ does not provide.

  2. frantic

    Another useful trick: !:1 expands to first argument of last command, !:2 is the second, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.