Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

syntax - What is bang dollar (!$) in Bash?

Bang dollar seems to refer to the last part of the last command line.

E.g.

$ ls -l
 .... something
$ !$
-l
bash: -l command not found

I can find plenty on the dollar variables (e.g. $!) but not on this. Any explanation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's the last argument of the previous command. From the documentation:

!!:$

designates the last argument of the preceding command. This may be shortened to !$.

Remark. If you want to play around with Bash's history, I suggest you turn on the shell option histverify like so:

shopt -s histverify

(you can also put it in your .bashrc to have it on permanently). When using history substitution, the substitution is not executed immediately; instead, it is put in readline's buffer, waiting for you to press enter… or not!


To make things precise, typing !$ is not equivalent to typing "$_": !$ is really a history substitution, refering to the last word of the previous command that was entered, whereas "$_" is the last argument of the previously executed command. You can compare both (I have shopt -s histverify):

$ { echo zee; }
zee
$ echo "$_"
zee
$ { echo zee; }
zee
$ echo !$
$ echo }

Also:

$ if true; then echo one; else echo two; fi
one
$ echo "$_"
one
$ if true; then echo one; else echo two; fi
$ echo !$
$ echo fi

And also:

$ echo zee; echo "$_"
zee
zee
$ echo zee2; echo !$
$ echo zee2; echo "$_"

And also

$ echo {1..3}
1 2 3
$ echo "$_"
3
$ echo {1..3}
1 2 3
$ echo !$
$ echo {1..3}

And also

$ echo one ;
$ echo "$_"
one
$ echo one ;
one
$ echo !$
$ echo ;

There are lots of other examples, e.g., with aliases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...