passing parameters to bash when executing a script fetched by curl

妖精的绣舞 提交于 2019-11-26 23:53:36

问题


I know how to execute remote bash script, via these syntaxes:

curl http://foo.com/script.sh | bash

or

bash < <( curl http://foo.com/script.sh )

which give the same result.

But what if I need to pass arguments to the bash script ? It's possible when the script is saved locally:

./script.sh argument1 argument2

I tried several possibilities like this one, without success:

bash < <( curl http://foo.com/script.sh ) argument1 argument2

回答1:


try

curl http://foo.com/script.sh | bash -s arg1 arg2

bash manual says:

If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.




回答2:


To improve on jinowolski's answer a bit, you should use:

curl http://example.com/script.sh | bash -s -- arg1 arg2

Notice the two dashes (--) which are telling bash to not process anything following it as arguments to bash.

This way it will work with any kind of arguments, e.g.:

curl -L http://bootstrap.saltstack.org | bash -s -- -M -N stable

This will of course work with any kind of input via stdin, not just curl, so you can confirm that it works with simple BASH script input via echo:

echo 'i=1; for a in $@; do echo "$i = $a"; i=$((i+1)); done' | \
bash -s -- -a1 -a2 -a3 --long some_text

Will give you the output

1 = -a1
2 = -a2
3 = -a3
4 = --long
5 = some_text



回答3:


Other alternatives:

curl http://foo.com/script.sh | bash /dev/stdin arguments
bash <( curl http://foo.com/script.sh ) arguments


来源:https://stackoverflow.com/questions/4642915/passing-parameters-to-bash-when-executing-a-script-fetched-by-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!