How do I get command name of the last executed command?
Solution 1
You can select particular word from last typed command with !!:
and a word designator. As a word designator you need 0
. You may find ^
and $
useful too. From man bash
:
Word Designators
0 (zero) The zeroth word. For the shell, this is the command word.
^ The first argument. That is, word 1.
$ The last argument.
So in your case try:
echo !!:0
Solution 2
In interactive mode, the easiest way to do this is just a keystroke combination alt+0 and alt+.. The shortcut alt+. means "recall n-th word from the previous line" (by default the last one) and alt+0 gives it an argument 0.
This should work for interactive bash on most systems (more generally, all shells that use readline
as its input library).
https://stackoverflow.com/questions/4009412/bash-how-to-use-arguments-from-previous-command
Related videos on Youtube
syntagma
Updated on September 18, 2022Comments
-
syntagma almost 2 years
Example: I type
man ls
, than I want to getman
only.By using
!!
I can getman ls
but how do I getman
?-
Edouard Fazenda over 9 yearsHi, you can try to use fc command ,
fc -rl | awk '{ print $2 }' | head -1
-
PM 2Ring over 9 years
!:0
will give you word 0 (the command word itself) of the previous command.
-
-
syntagma over 9 yearsSecond comment under my question mentions:
!:0
, what is the difference between!:0
and!!:0
? -
cuonglm over 9 yearsYou can use
!:0
, and note inbash
, ifextglob
is enabled, it must not follow by blank, newline,\r
, = or (. -
jimmij over 9 years@REACHUS
!
starts history substitution and!!
refer to previous command. Probably single!
in some cases denotes!!
and indeedecho !:0
works butecho !
doesn't while!!
works in both cases. -
muru over 9 yearsYou don't need to
echo
. You can use thep
modifier instead:!!:0:p