Sea*_*ene 37 command-line bash
bash -
以下 bash shell 代码是什么意思?好像是用来把输出最后一个代码作为输入。如果是这样,我可以把它写成bash
orxargs bash
吗?
curl --silent --location https://rpm.nodesource.com/setup | bash -
Run Code Online (Sandbox Code Playgroud)
200*_*ess 39
如有疑问,请阅读源代码。=)
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);
Run Code Online (Sandbox Code Playgroud)
换句话说,-
就是说没有更多的选择。如果命令行上还有更多单词,它们将被视为文件名,即使单词以-
.
当然,在您的示例中,这-
完全是多余的,因为无论如何都没有任何内容。换句话说,bash -
完全等同于bash
。
Bash 接受它的命令
这是一种误解,它bash -
告诉 Bash 从其标准输入读取其命令。虽然这是事实,在你的榜样,bash将标准输入读取它的命令,就已经这样做了,无论是否有一个-
命令行上,因为,如上所述,bash -
是相同的bash
。
为了进一步说明这-
并不意味着标准输入,请考虑:
该cat
命令旨在将 a 解释-
为标准输入。例如:
$ 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
Run Code Online (Sandbox Code Playgroud)相反,你不能击来执行/bin/date
,然后/bin/hostname
通过尝试这样的:
$ echo date | bash - hostname
/bin/hostname: /bin/hostname: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)
相反,它试图解释/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
Run Code Online (Sandbox Code Playgroud)你能改写xargs bash
吗?号 curl | xargs bash
将调用的bash的脚本作为命令行参数的内容。内容的第一个词将是第一个参数,它可能会被误解为脚本文件名。