“bash – ”中的“ – ”是什么意思?

什么是bash -在下面的bash shell代码中意味着什么? 它似乎用于将输出的最后一个代码作为输入。 如果是这样,我可以把它写成bashxargs bash吗?

 curl --silent --location https://rpm.nodesource.com/setup | bash - 

如有疑问,请阅读源代码。 =)

Bash 4.3, shell.c第830行,在函数parse_shell_options()

  /* A single `-' signals the end of options. From the 4.3 BSD sh. An option `--' means the same thing; this is the standard getopt(3) meaning. */ if (arg_string[0] == '-' && (arg_string[1] == '\0' || (arg_string[1] == '-' && arg_string[2] == '\0'))) return (next_arg); 

换句话说, -说是没有更多的选择 。 如果在命令行上有更多的单词,它们将被视为文件名,即使单词以-开头。

当然,在你的例子中,这完全是多余的,因为无论如何都没有任何关注。 换句话说, bash -完全等同于bash


Bash接受命令

  1. 如果在命令行中提供,则从脚本文件中,或
  2. 如果stdin不是TTY,那么从stdin非交互式(例如在你的例子中:stdin是一个管道,所以Bash会将该URL的内容作为脚本执行),或者
  3. 交互式,如果它的标准输入是TTY。

bash -告诉Bash从其标准输入中读取命令是一种误解。 虽然在你的例子中,Bash会从stdin中读取它的命令,但无论是否在命令行上都有- ,因为如上所述, bash -bash相同。

为了进一步说明-并不意味着stdin,请考虑:

  • cat命令旨在将 – 解释为stdin。 例如:

     $ echo xxx | cat /etc/hosts - /etc/shells 127.0.0.1 localhost xxx # /etc/shells: valid login shells /bin/sh /bin/dash /bin/bash /bin/rbash /bin/zsh /usr/bin/zsh /usr/bin/screen /bin/tcsh /usr/bin/tcsh /usr/bin/tmux /bin/ksh93 
  • 相反,你不能通过尝试这个来让Bash执行/bin/date然后/bin/hostname

     $ echo date | bash - hostname /bin/hostname: /bin/hostname: cannot execute binary file 

    相反,它试图将/bin/hostname解释为shell脚本文件,但由于它是一堆二进制gobbledygook而失败。

  • 你不能使用bash -执行date +%s bash -或者。

     $ date +%s 1448696965 $ echo date | bash - Sat Nov 28 07:49:31 UTC 2015 $ echo date | bash - +%s bash: +%s: No such file or directory 

你能写一些xargs bash吗? curl | xargs bash curl | xargs bash将使用脚本的内容作为命令行参数调用bash。 内容的第一个单词是第一个参数,它可能被误解为脚本文件名。