Bash 中的 bang 美元 (!$) 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41385015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is bang dollar (!$) in Bash?
提问by Snowcrash
Bang dollar seems to refer to the last part of the last command line.
Bang Dollar 似乎指的是最后一个命令行的最后一部分。
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?
我可以在美元变量(例如$!
)上找到很多,但在此上找不到。有什么解释吗?
回答by gniourf_gniourf
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:
评论。如果你想玩转 Bash 的历史,我建议你histverify
像这样打开 shell 选项:
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!
(你也可以把它放在你的身上.bashrc
以永久使用它)。使用历史替换时,替换不会立即执行;相反,它被放在 readline 的缓冲区中,等待您按 Enter 键……或者不!
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 executedcommand. You can compare both (I have shopt -s histverify
):
准确地说,打字!$
不等同于打字"$_"
:!$
实际上是一个历史替换,指的是输入的上一个命令的最后一个单词,而"$_"
是上一个执行命令的最后一个参数。你可以比较两者(我有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.
还有很多其他的例子,例如,别名。
回答by zee
!$
can do what $_
does, except the fact that $_
does not store the value it returns (as its substitution) to history
.
!$
可以做什么$_
,除了$_
不存储它返回的值(作为它的替代)到history
.
Here is an example.
这是一个例子。
With !$
和 !$
za:tmep za$ ls -lad
drwxr-xr-x 4 za staff 136 Apr 6 2016 .
za:tmep za$ !$
-lad
-bash: -lad: command not found
za:tmep za$ history | tail -n 3
660 ls -lad
661 -lad <<== history shows !$ substitution.
662 history | tail -n 3
With $_
和 $_
za:tmep za$ ls -lad
drwxr-xr-x 4 za staff 136 Apr 6 2016 .
za:tmep za$ $_
-bash: -lad: command not found
za:tmep za$ history | tail -n 3
663 ls -lad
664 $_ <<== history shows $_ and not its substitution.
665 history | tail -n 3
za:tmep za$
More options:
更多选择:
!^ first argument
!:2 second argument
!:2-$ second to last arguments
!:2* second to last arguments
!:2- second to next to last arguments
!:2-3 second to third arguments
!$ last argument
!* all arguments
回答by Mike D3ViD Tyson
Monkey's answer:
猴子的回答:
whit !$ you can easily print the last wordof the previous command
白衣!美元,你可以轻松打印最后一个字的的前一个命令
#Create new file
touch newfile.txt
#Edit new file using !$ instead newfile.txt again
nano !$