What does bash -
mean in the following bash shell code? It seems to be used to take the output the last code as the input. If so, can I just write it as bash
or xargs bash
?
curl --silent --location https://rpm.nodesource.com/setup | bash -
bashcommand line
What does bash -
mean in the following bash shell code? It seems to be used to take the output the last code as the input. If so, can I just write it as bash
or xargs bash
?
curl --silent --location https://rpm.nodesource.com/setup | bash -
Best Answer
When in doubt, read the source code. =)
Bash 4.3,
shell.c
line 830, in functionparse_shell_options()
:In other words, the
-
is saying that there are no more options. If there were any more words on the command line, they would be treated as a filename, even if the word started with a-
.In your example, of course, that
-
is completely redundant, as there is nothing following it anyway. In other words,bash -
is exactly equivalent tobash
.Bash takes its commands
It is a misconception that
bash -
tells Bash to read its commands from its standard input. While it is true that in your example, Bash will read its commands from stdin, it would have done so regardless of whether there was a-
on the command line, because, as stated above,bash -
is identical tobash
.To further illustrate that
-
does not mean stdin, consider:The
cat
command is designed to interpret a-
as stdin. For example:In contrast, you can't get Bash to execute
/bin/date
then/bin/hostname
by trying this:Rather, it tries to interpret
/bin/hostname
as a shell script file, which fails because it's a bunch of binary gobbledygook.You can't execute
date +%s
usingbash -
either.Can you write
xargs bash
instead? No.curl | xargs bash
would invoke bash with the contents of the script as command-line arguments. The first word of the contents would be the first argument, and it would likely be misinterpreted as a script filename.