Bash中的美元(!$)是什么? [英] What is bang dollar (!$) in Bash?

查看:444
本文介绍了Bash中的美元(!$)是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美元似乎是指最后一个命令行的最后一部分.

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

例如

$ 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?

推荐答案

这是上一个命令的最后一个参数.从文档:

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 !$.

备注.如果您想了解Bash的历史,建议您像下面这样打开外壳选项histverify:

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

(您也可以将其放入.bashrc中以使其永久启用).使用历史记录替换时,不会立即执行替换;而是将其放置在readline的缓冲区中,等待您按Enter键.

(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!

为了精确起见,键入!$并不等同于键入"$_":!$实际上是历史记录的替代品,指的是前一个命令的最后一个词,输入,而"$_"是先前执行命令的最后一个参数.您可以将两者进行比较(我有shopt -s histverify):

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 }

也:

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

还有:

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

还有

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

还有

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

还有很多其他示例,例如,带有别名.

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

这篇关于Bash中的美元(!$)是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆