有没有办法将 arguments/options 传递给管道执行的脚本?

Is there a way to pass arguments/options to pipeline-executed script?

我们知道可以在bash中进行以下操作:

curl -Ls example.com/script.sh | bash

但是我们可以传递一些参数吗?

curl -Ls example.com/script.sh | bash --option1

在这种情况下,bash 将采用该选项。可以通过某种方法将其传递给脚本吗?

这就是 -s 选项的用途:

curl -Ls example.com/script.sh | bash -s -- --option1

由于 -s 明确告诉 bash 从标准输入读取它的命令,它不会尝试将它的第一个参数解释为从中读取命令的文件。相反,所有参数都用于设置位置参数。

或者,您可以使用进程替换而不是直接从标准输入读取。

bash <(curl -Ls example.com/script.sh) --option1

你可以这样做:

( echo 'set -- --option1' && curl -Ls example.com/script.sh ) | bash 

在输入前添加一个 set 命令。