How can I recall the argument of the previous bash command?
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 numbern
fromhistory
-
!pattern
- most recent command matchingpattern
-
!!:s/find/replace
- last command, substitutefind
withreplace
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.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 24, 2022Comments
-
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 bygcc file.c
.Is there a way in Bash to recall the argument of the previous command?
-
janmoesen almost 14 yearsAlso, 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 -
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 over 10 yearsI 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 over 10 yearsI get the same thing if there weren't two arguments in the previous line. Eg. line 1
ls a.txt
line 2ll !:1-2
-
RNA over 10 yearsyou're right. That is a stupid mistake I made. thanks!
-
Will over 8 yearsSimilar to
!$
, you use!^
for the first argument. -
Big McLargeHuge about 7 yearsInstead of
!!:s/find/replace
, you can also^find^replace
. -
jx12345 about 7 yearsahh... *nix... you are a thing of beauty... everyday I love you more
-
816-8055 almost 7 yearsAlso to get all of the arguments,
!:^-$
can be used or of course similar code in combination with numbers. -
Chan Kim over 6 yearsHow can we refer to the second to last argument of the previous command? for example, If I gave
echo tiger rabbit
, how can I refertiger
at the following command? -
Brian McCutchon over 5 years
Alt + .
doesn't work in vi mode. Just FYI, for others who were confused here. -
Bucket over 5 yearsBe aware of the key word "last," especially if your command contained multiple arguments.
-
Bucket over 5 yearsI've always found the
↑
and↓
keys to work as well. -
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 about 5 years[sighs]... what a wonderful way to be distracted at work - just love this
-
林果皞 about 5 yearsNote that
!$
print the full command in the first line when run, while$_
doesn't. -
Aphoid about 4 yearsAlso:
!*
- all arguments from the previous command (after the program/built-in/script). e.g.:ls *.tmp *.cache rm !*
-
young_souvlaki about 4 yearsThis is truly incredible. I love the
M-.
feature. Where is this documented? -
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#TOCDocumentation -
hundredrab over 3 yearsIs there a way I can make
<alt> + .
work in vi mode? is there a similar workaround for zsh? -
Chen over 3 yearshow does it work on mac? I use mac + iterm2, press Alt + . only shows "≥"
-
Jonas Eberle about 3 years
!:-3
or!:3-
works also (for all arguments until/from the 3rd argument) -
Felix over 2 years@Chen have to use osx at the moment, have not yet found a shortcut solution
-
Motti Shneor over 2 yearsFor 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 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."