(bash)的脚本之间传递参数用空格 [英] Passing arguments with spaces between (bash) script

查看:138
本文介绍了(bash)的脚本之间传递参数用空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的bash两个脚本

I've got the following bash two scripts

a.sh:

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

b.sh:

#!/bin/bash
someApp $*

该someApp二进制接收 $ * 作为2个参数,而不是1('我'和'参数')

The someApp binary receives $* as 2 arguments ('My' and 'Argument') instead of 1.

我测试过的几件事情:


  • 运行someApp只有直通 b.sh 按预期工作

  • 迭代+回响在 b.sh 参数按预期工作

  • 使用 $ @ 而不是 $ * 不有所作为

  • 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 接收一个参数引用它。

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

someApp "$*"

这有可能是您要使用 $ @ 代替,让 someApp 将接受两个参数,如果你是调用 b.sh

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'

使用 someApp$ * someApp 将接受一个参数我的第一个我的第二个。随着 someApp$ @ someApp 将接受两个参数,我的第一个我的第二个

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

这篇关于(bash)的脚本之间传递参数用空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆