在(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

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

Passing arguments with spaces between (bash) script

bash

提问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.shworks as expected
  • Iterate+echo the arguments in b.shworks 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 someAppreceives a single argument.

$*,不加引号,扩展为两个词。您需要引用它以便someApp接收单个参数。

someApp "$*"

It's possible that you want to use $@instead, so that someAppwould receive two arguments if you were to call b.shas

这有可能是您要使用$@代替,这样someApp如果你要打电话将获得两个参数b.sh

b.sh 'My first' 'My second'

With someApp "$*", someAppwould receive a single argument My first My second. With someApp "$@", someAppwould receive two arguments, My firstand My second.

使用someApp "$*",someApp将接收一个参数My first My second。使用someApp "$@",someApp将接收两个参数,My firstMy second