How can I recall the argument of the previous bash command?

111,336

Solution 1

You can use $_ or !$ to recall the last argument of the previous command.

Also Alt + . can be used to recall the last argument of any of the previous commands.

Solution 2

If the previous command had two arguments, like this

ls a.txt b.txt

and you wanted the first one, you could type

!:1

giving

a.txt

Or if you wanted both, you could type

!:1-2

giving

a.txt b.txt

You can extend this to any number of arguments, eg:

!:10-12

Solution 3

!!:n where n is the 0-based position of the argument you want.

For example:

echo 'one' 'two'
# "one two"

echo !!:2
# "two"

The ! prefix is used to access previous commands.

Other useful commands:

  • !$ - last argument from previous command
  • !^ - first argument (after the program/built-in/script) from previous command
  • !* - all arguments from previous command
  • !! - previous command (often pronounced "bang bang")
  • !n - command number n from history
  • !pattern - most recent command matching pattern
  • !!:s/find/replace - last command, substitute find with replace

More info on command history

Solution 4

In the command-line, you can press alt+. or esc-.

It cycles through the last argument of your previous commands.

Solution 5

If you know the number given in the history for a particular command, you can pretty much take any argument in that command using following terms.

Use following to take the second argument from the third command in the history,

!3:2

Use following to take the third argument from the fifth last command in the history,

!-5:3

Using a minus sign, you ask it to traverse from the last command of the history.

Share:
111,336
Admin
Author by

Admin

Updated on July 24, 2022

Comments

  • Admin
    Admin almost 2 years

    Is there a way in Bash to recall the argument of the previous command?

    I usually do vi file.c followed by gcc file.c.

    Is there a way in Bash to recall the argument of the previous command?

  • janmoesen
    janmoesen almost 14 years
    Also, if you want an arbitrary argument, you can use !!:1, !!:2, etc. (!!:0 is the previous command itself.) See gnu.org/software/bash/manual/bashref.html#History-Interactio‌​n
  • Robert Gowland
    Robert Gowland over 10 years
    @RNA, I just tried it again to make sure I didn't include a typo, could you provide a little more detail (eg. ubuntu command line, cygwin for windows? error message? previous line?)
  • RNA
    RNA over 10 years
    I am using GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13) Copyright (C) 2007 Free Software Foundation, Inc. The error message is -bash: :1-2: bad word specifier
  • Robert Gowland
    Robert Gowland over 10 years
    I get the same thing if there weren't two arguments in the previous line. Eg. line 1 ls a.txt line 2 ll !:1-2
  • RNA
    RNA over 10 years
    you're right. That is a stupid mistake I made. thanks!
  • Will
    Will over 8 years
    Similar to !$, you use !^ for the first argument.
  • Big McLargeHuge
    Big McLargeHuge about 7 years
    Instead of !!:s/find/replace, you can also ^find^replace.
  • jx12345
    jx12345 about 7 years
    ahh... *nix... you are a thing of beauty... everyday I love you more
  • 816-8055
    816-8055 almost 7 years
    Also to get all of the arguments, !:^-$ can be used or of course similar code in combination with numbers.
  • Chan Kim
    Chan Kim over 6 years
    How can we refer to the second to last argument of the previous command? for example, If I gave echo tiger rabbit, how can I refer tiger at the following command?
  • Brian McCutchon
    Brian McCutchon over 5 years
    Alt + . doesn't work in vi mode. Just FYI, for others who were confused here.
  • Bucket
    Bucket over 5 years
    Be aware of the key word "last," especially if your command contained multiple arguments.
  • Bucket
    Bucket over 5 years
    I've always found the and keys to work as well.
  • everyonesdesign
    everyonesdesign about 5 years
    @Bucket keys go though previous commands, while the solution provided by Antonio allows to go through previous arguments (last argument of each previous command only)
  • user115014
    user115014 about 5 years
    [sighs]... what a wonderful way to be distracted at work - just love this
  • 林果皞
    林果皞 about 5 years
    Note that !$ print the full command in the first line when run, while $_ doesn't.
  • Aphoid
    Aphoid about 4 years
    Also: !* - all arguments from the previous command (after the program/built-in/script). e.g.: ls *.tmp *.cache rm !*
  • young_souvlaki
    young_souvlaki about 4 years
    This is truly incredible. I love the M-. feature. Where is this documented?
  • Chris
    Chris almost 4 years
    @young_souvlaki see man bash, quite literally everything is in that, you just have to actually read it (which is the hard part, but look under 'HISTORY EXPANSION' and 'Commands for Manipulating the History' in particular. For GNU readline related stuff (which is most key binding related stuff outside of bash): tiswww.cwru.edu/php/chet/readline/rltop.html#TOCDocumentatio‌​n
  • hundredrab
    hundredrab over 3 years
    Is there a way I can make <alt> + . work in vi mode? is there a similar workaround for zsh?
  • Chen
    Chen over 3 years
    how does it work on mac? I use mac + iterm2, press Alt + . only shows "≥"
  • Jonas Eberle
    Jonas Eberle about 3 years
    !:-3 or !:3- works also (for all arguments until/from the 3rd argument)
  • Felix
    Felix over 2 years
    @Chen have to use osx at the moment, have not yet found a shortcut solution
  • Motti Shneor
    Motti Shneor over 2 years
    For some reason, on MacOS 11.6.1 Terminal, zsh 5.8 (x86_64-apple-darwin20.0) This doesn't work. It looks like zsh ignores the '-' sign - and takes n simply from the start of list. What to do? which 'man' page to consult?
  • cilix
    cilix about 2 years
    @Chen Try using ESC . See the bash man page, under "Readline Notation": "[...] Similarly, meta keys are denoted by M-key, so M-x means Meta-X. (On keyboards without a meta key, M-x means ESC x, i.e., press the Escape key then the x key. This makes ESC the meta prefix."