简体   繁体   English

在bash中使用`!#`有什么用?

[英]What's the use of `!#` in bash?

I'm not talking about the UNIX shebang meant to give the path of an interpreter. 我不是在谈论UNIX shebang意图给出解释器的路径。 In fact, the shebang has made it quite hard to google this question successfully... 事实上,shebang已经很难成功地谷歌这个问题了...

I'm talking about bash expansion: 我在谈论bash扩展:

  • !! retrieves the immediately preceding command (akin to the up arrow, but more usable) 检索前一个命令(类似于向上箭头,但更有用)
  • !$ retrieves the last word of the last command !$检索最后一个命令的最后一个单词
  • !# appears to retrieve the current line !#似乎检索当前行

But what're actually useful uses of !# ? 实际上有用的是什么!#

  • echo !# expands to echo echo echo !# #extens to echo echo
  • echo echo !# expands to echo echo echo echo echo echo !# #extenss to echo echo echo echo
  • ls !# expands to ls ls . ls !#扩展到ls ls

All of which seem overwhelmingly pointless. 所有这些看起来都是毫无意义的。 All I know about the !# command is what I've learned from using it – I'm not certain that I know its true functionality. 我所知道的!#命令是我从使用它学到的东西 - 我不确定我是否知道它的真正功能。

!# is The entire command line typed so far . !# #is到目前为止输入的整个命令行 From man bash : 来自man bash

Event Designators
   An event designator is a reference to a command line entry in the history list.

   !      Start a history substitution, except when followed by a blank, newline, carriage return,  =  or  (
          (when the extglob shell option is enabled using the shopt builtin).
   !n     Refer to command line n.
   !-n    Refer to the current command line minus n.
   !!     Refer to the previous command.  This is a synonym for `!-1'.
   !string
          Refer to the most recent command starting with string.
   !?string[?]
          Refer  to  the  most recent command containing string.  The trailing ? may be omitted if string is
          followed immediately by a newline.
   ^string1^string2^
          Quick substitution.  Repeat the last command,  replacing  string1  with  string2.   Equivalent  to
          ``!!:s/string1/string2/'' (see Modifiers below).
   !#     The entire command line typed so far.

One example of usage: 用法的一个例子:

$ mkdir foo && cd !#:1

Makes the directory foo and changes to it. 使目录foo并更改它。

As mentioned comments on the question and other answer(s), you can easily find the section of man bash : 正如所提到的关于问题和其他答案的评论,你可以很容易地找到man bash的部分:

!#     The entire command line typed so far.

What isn't made explicit by the man pages or other documentation and perhaps leading you your befuddlement to a useful use for !# is that bash event designators are meant to be used in combination with the word designators and modifiers (found just below event designators on the man page). 人工页面或其他文档没有明确说明的内容,也许可以引导您将其迷失为有用的用途!# #bash事件指示符旨在与单词指示符和修饰符结合使用(在事件指示符下面找到)在手册页上)。 The use cases also make more sense when chaining commands together with pipes, ; 将命令与管道链接在一起时,用例也更有意义; or & / && & / &&

For example: 例如:

I could use the substitution modifier to run a script foo.sh against two inputs bar and baz : 我可以使用替换修饰符对两个输入barbaz运行脚本foo.sh

$ ./foo.sh bar; !#:s/bar/baz/

expands to form: 扩展形成:

./foo.sh bar; ./foo.sh baz;

Or an example from the Docs (RIP Docs) reproduced below shows how to use the word selector :N to select the first argument to the command as typed so far and neatly cd into the just created directory: 或者下面转载的文档(RIP文档)中的示例显示了如何使用单词selector :N来选择命令的第一个参数到目前为止输入并整齐地cd到刚刚创建的目录中:

$ mkdir backup_download_directory && cd !#:1
mkdir backup_download_directory && cd backup_download_directory

These examples are pretty trivial, but I hope they show how !# could really save somebody some serious redundant typing while crafting powerful one-liners. 这些例子非常简单,但我希望它们能够显示出来!#在制作强大的单行程序时,可以真正为某些人节省一些严重的冗余打字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM