在(bash)脚本之间传递带有空格的参数

我有下面的 bash 两个脚本

嘘:

#!/bin/bash
./b.sh 'My Argument'

B.sh:

#!/bin/bash
someApp $*

SomApp 二进制文件接收 $*作为2个参数(‘ My’和‘ Argument’) ,而不是1。

我测试了几样东西:

  • 只通过 b.sh运行一些应用程序就可以正常工作
  • 迭代 + 回显 b.sh中的参数,正如预期的那样
  • 使用 $@而不是 $*并没有什么不同
106263 次浏览

$*, unquoted, expands to two words. You need to quote it so that someApp receives a single argument.

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

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.