Command History – How to Get Command Name of the Last Executed Command

command historycommand linehistory-expansionshell

Example: I type man ls, than I want to get man only.

By using !! I can get man ls but how do I get man?

Best Answer

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