SyndicatorBBB
SyndicatorBBB

Reputation: 1807

What is the unix history command "!$" in bash?

I'm trying to understand what is the unix command !$.

For example, I know that the command !1 is used to run the history command number 1.

It seems like !$ runs the last command typed in the bash.

For example if I wrote mv folder12 folder123 and then I would write cd !$ I would actually preform cd folder123.

Is it correct that !$ runs the last command typed in the bash?

Upvotes: 2

Views: 991

Answers (1)

fedorqui
fedorqui

Reputation: 289675

!$ matches the last argument of the previous command.

From man bash

yank-last-arg (M-., M-_)

Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the "!$" history expansion had been specified.

Example

$ vi a
$ ls -l !$ # expands to "ls -l a"
-rw-rw-r-- 1 me me 30 18 abr.  22:00 a

See also What is your single most favorite command-line trick using Bash?:

I'm a fan of the !$, !^ and !* expandos, returning, from the most recent submitted command line: the last item, first non-command item, and all non-command items. To wit (Note that the shell prints out the command first).

And also a good reading: The Definitive Guide to Bash Command Line History.

Upvotes: 5

Related Questions