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?
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?
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.
!!:1
, !!:2
, etc. (!!:0
is the previous command itself.) See gnu.org/software/bash/manual/bashref.html#History-Interaction
Commented
Jul 30, 2010 at 12:21
Alt + .
doesn't work in vi mode. Just FYI, for others who were confused here.
Commented
Nov 1, 2018 at 1:32
!$
print the full command in the first line when run, while $_
doesn't.
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
-bash: :1-2: bad word specifier
ls a.txt
line 2 ll !:1-2
Commented
Feb 15, 2014 at 17:35
!!: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
!!:s/find/replace
, you can also ^find^replace
.
Commented
Apr 16, 2017 at 18:28
!*
- all arguments from the previous command (after the program/built-in/script). e.g.: ls *.tmp *.cache rm !*
In the command-line, you can press alt+. or esc-.
It cycles through the last argument of your previous commands.
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.
!* runs a new command with all previous arguments.
ls /tmp
cd !*
#you are now in /tmp
$_
instead.
Commented
May 22, 2019 at 16:11
Yes, you can use !$
to recall the last argument of the preceding command.
There are two different ways to recall command history:
man bash
, search for HISTORY EXPANSION
.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)
echo
in our examples)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.