在(bash)脚本之间传递带有空格的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17094086/
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
Passing arguments with spaces between (bash) script
提问by John Fear
I've got the following bash two scripts
我有以下 bash 两个脚本
a.sh:
灰:
#!/bin/bash
./b.sh 'My Argument'
b.sh:
b.sh:
#!/bin/bash
someApp $*
The someApp binary receives $*
as 2 arguments ('My' and 'Argument') instead of 1.
someApp 二进制文件接收$*
2 个参数(“My”和“Argument”)而不是 1。
I've tested several things:
我已经测试了几件事:
- Running someApp only thru
b.sh
works as expected - Iterate+echo the arguments in
b.sh
works as expected - Using
$@
instead of$*
doesn't make a difference
- 仅通过运行 someApp 可按
b.sh
预期工作 b.sh
按预期迭代+回显工作中的参数- 使用
$@
而不是$*
没有区别
回答by chepner
$*
, unquoted, expands to two words. You need to quote it so that someApp
receives a single argument.
$*
,不加引号,扩展为两个词。您需要引用它以便someApp
接收单个参数。
someApp "$*"
It's possible that you want to use $@
instead, so that someApp
would receive two arguments if you were to call b.sh
as
这有可能是您要使用$@
代替,这样someApp
如果你要打电话将获得两个参数b.sh
为
b.sh 'My first' 'My second'
With someApp "$*"
, someApp
would receive a single argument My first My second
. With someApp "$@"
, someApp
would receive two arguments, My first
and My second
.
使用someApp "$*"
,someApp
将接收一个参数My first My second
。使用someApp "$@"
,someApp
将接收两个参数,My first
和My second
。