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

Searene

bash -以下bash shell代码是什么意思?它似乎用于将输出的最后一个代码作为输入。如果是这样,我可以只写为bash还是xargs bash

curl --silent --location https://rpm.nodesource.com/setup | bash -
200_成功

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

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);

换句话说,这-就是说没有更多选择了如果命令行上还有其他单词,即使该单词以a开头,它们也将被视为文件名-

当然,在您的示例中,这-完全是多余的,因为无论如何都没有遵循它。换句话说,bash -与完全等效bash


Bash接受命令

  1. 从脚本文件(如果在命令行中提供),或者
  2. 如果它的stdin不是TTY(例如在您的示例中:stdin是一个管道,那么Bash将以脚本的形式执行该URL的内容),则从其stdin非交互地进行操作,或者
  3. 如果其stdin是TTY,则以交互方式进行。

这是一种误解,bash -告诉Bash从其标准输入中读取其命令。虽然这事实,在你的榜样,bash将标准输入读取它的命令,就已经这样做了,无论是否有一个-命令行上,因为,如上所述,bash -是相同的bash

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

  • cat命令旨在将a解释-为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
    
  • 相反,你不能击来执行/bin/date,然后/bin/hostname通过尝试这样的:

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

    而是尝试将其解释/bin/hostname为shell脚本文件,该文件失败,因为它是一堆二进制gobbledygook。

  • 您不能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将使用脚本内容作为命令行参数来调用bash。内容的第一个单词将是第一个参数,并且可能会被误解为脚本文件名。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章