bash 通过管道时传递脚本的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14693100/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:27:06  来源:igfitidea点击:

Pass args for script when going thru pipe

bash

提问by PeterMmm

I have this example shell script:

我有这个示例 shell 脚本:

echo //

So I may call

所以我可以打电话

$ . ./script 5
# output: /5/

I want to pipe the script into sh(ell), but can I pass the arg too ?

我想将脚本通过管道传输到 sh(ell),但是我也可以传递 arg 吗?

cat script | sh 
# output: //

回答by Anton Kovalenko

You can pass arguments to the shell using the -soption:

您可以使用以下-s选项将参数传递给 shell :

cat script | bash -s 5

回答by northtree

Use bash -s -- <args>

bash -s -- <args>

e.g, install google cloud sdk

例如,安装谷歌云 SDK

~ curl https://sdk.cloud.google.com | bash -s -- --disable-prompts

~ curl https://sdk.cloud.google.com | bash -s -- --disable-prompts