421

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?

2

8 Answers 8

743

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.

13
  • 158
    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-Interaction
    – janmoesen
    Commented Jul 30, 2010 at 12:21
  • 63
    Similar to !$, you use !^ for the first argument.
    – Will
    Commented Oct 26, 2015 at 21:22
  • 28
    ahh... *nix... you are a thing of beauty... everyday I love you more
    – jx12345
    Commented May 26, 2017 at 12:36
  • 10
    Alt + . doesn't work in vi mode. Just FYI, for others who were confused here. Commented Nov 1, 2018 at 1:32
  • 4
    Note that !$ print the full command in the first line when run, while $_ doesn't.
    – 林果皞
    Commented Jun 17, 2019 at 20:25
205

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
7
  • @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?) Commented Feb 14, 2014 at 14:59
  • 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
    – RNA
    Commented Feb 14, 2014 at 18:21
  • 3
    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 Commented Feb 15, 2014 at 17:35
  • you're right. That is a stupid mistake I made. thanks!
    – RNA
    Commented Feb 15, 2014 at 21:04
  • 2
    [sighs]... what a wonderful way to be distracted at work - just love this
    – user115014
    Commented May 22, 2019 at 16:14
157

!!: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
3
  • 10
    Instead of !!:s/find/replace, you can also ^find^replace. Commented Apr 16, 2017 at 18:28
  • 5
    Also: !* - all arguments from the previous command (after the program/built-in/script). e.g.: ls *.tmp *.cache rm !*
    – Aphoid
    Commented May 4, 2020 at 16:53
  • 1
    Dead links usually earn downvotes, but given the high-quality answer, I will supply a Web Archive link web.archive.org/web/20200211203800/https://www.washington.edu/…
    – gbarry
    Commented Nov 14, 2023 at 18:05
87

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

It cycles through the last argument of your previous commands.

2
  • 1
    I've always found the and keys to work as well.
    – Bucket
    Commented Nov 29, 2018 at 14:44
  • 6
    @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) Commented Apr 18, 2019 at 12:16
33

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.

1
  • 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? Commented Oct 29, 2021 at 8:55
25

!* runs a new command with all previous arguments.

ls /tmp
cd !*
#you are now in /tmp
2
  • 2
    This didn't work for me on OSX - had to use $_ instead.
    – user115014
    Commented May 22, 2019 at 16:11
  • This should be the answer. Commented May 10, 2021 at 12:35
22

Yes, you can use !$ to recall the last argument of the preceding command.

1
  • 3
    Be aware of the key word "last," especially if your command contained multiple arguments.
    – Bucket
    Commented Nov 29, 2018 at 14:41
0

There are two different ways to recall command history:

  • Bash history expansion, in which a sequence of characters you input is expanded into part of your command history when you run the command (most other examples in answers to this question are in this category)
    • Documentation: man bash, search for HISTORY EXPANSION.
  • Readline commands, in which a sequence of keystrokes you input causes part of your command history to be inserted into the current command before you run it.
    • Documentation: man bash, search for Readline Command Names, Commands for Manipulating the History and Numeric Arguments.

Readline Commands

If we have previously run the following commands:

echo A B C
echo D E F

Use these methods to insert the following arguments into your current command for example:

  • F: Alt + . (inserts the last argument of the previous command)
  • C: Alt + . , Alt + . (cycles through last arguments of earlier commands)
  • E: Alt + 2 , Alt + . (specify the second argument 2, before inserting from last command)
  • B: Alt + 2 , Alt + . , Alt + . (As above, cycles through earlier commands)
    • The numeric argument before Alt+. can get you any single argument from any previous command
    • Argument numbers start from 0 (echo in our examples)
    • Argument numbers can have multiple digits
    • Argument numbers can be negative, in which case the count backward from the last argument in the command
    • Note that Alt + digit may be assigned to a keyboard shortcut to select a tab in your shell GUI, which will prevent these combinations being passed to Bash.
  • A: Alt + Ctrl + y, This is a special case; it inserts the “first” argument (ie. the second string in the command) of the last command and does not then cycle through earlier commands.

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