Answers:
如有疑问,请阅读源代码。=)
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接受命令
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。内容的第一个单词将是第一个参数,并且可能会被误解为脚本文件名。
An argument of - is equivalent to --.
xargs它,则可以使用(在输入脚本足够小的情况下)使用| xargs bash -c; 但这确实不是的有用或惯用用法xargs。