Bash - 如何将参数传递给通过标准输入读取的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8514284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash - How to pass arguments to a script that is read via standard input
提问by dabest1
I am trying execute a script from standard input and also pass arguments to it. Is there a way to do it?
我正在尝试从标准输入执行脚本并将参数传递给它。有没有办法做到这一点?
Let's say that I have the following:
假设我有以下几点:
cat script.sh | bash
How would I pass the arguments to the script?
我将如何将参数传递给脚本?
I do not want to do this:
我不想这样做:
bash script.sh arguments
Nor this:
也不是这个:
./script.sh arguments
回答by Michael Hoffman
On Linux,
在 Linux 上,
cat script.sh | bash /dev/stdin arguments
seems to work.
似乎工作。
回答by bryant1410
After @ccarton's comment:
cat script.sh | bash -s - arguments
It's more portable than @Michael Hoffman's solution.
它比@Michael Hoffman 的解决方案更便携。

