dylan12345 dylan12345 - 4 months ago 19
Bash Question

Bash !$ equivalent for first word

Is there a way for Bash to return the first word of the previous command (the command name itself), similar to how

!$
returns the last word of the previous command?

For instance:

ls -l


I would like to return
ls

Answer

You can choose a word by number with !:. This includes any pre-command assignments, so the exact number you need varies.

$ x=3 some_command foo   # some_command is !:1
$ ls -l                  # ls is !:0
$ x=6 y=3 bar            # bar is !:2