如何通过从bash脚本到bash脚本空间的说法? [英] How to pass an argument with space from bash script to bash script?
问题描述
我有一个脚本的操作数据,创建参数并将其发送到第二个脚本。其中一个参数包含空格。
I have a script the manipulates data, creates arguments and sends them to a second script. One of the arguments contains a space.
script1.sh:
script1.sh:
args=()
args+=("A")
args+=("1 2")
args+=("B")
. script2.sh ${args[@]}
script2.sh:
script2.sh:
for f in "$@"
do
echo f=$f
done
我想获得1 2
单参数,而是单独接受他们:
I'd like to get "1 2"
as single argument, but receive them separately:
f=A
f=1
f=2
f=B
我试过也将输入的SCRIPT2列出在=($ @)
,并使用了它遍历为$ F {在[ @]}
,但得到了同样的结果。
I tried also converting the input in script2 to list in=($@)
and iterating over it using for f in ${in[@]}
but got the same result.
所以,这个问题可能是下列之一:建筑的参数列表;通过内置列表;解析输入;或遍历输入。
So, the problem might be in one of the following: building the list of arguments ; passing the built list ; parsing the input ; or iterating over the input.
如何将参数传递从bash脚本到bash脚本空间?感谢。
*我使用的是Windows上的git-bash的。
* I'm using git-bash on Windows.
推荐答案
您只需要添加传递数组作为参数时报价:
You just need to add quotes when passing the array as an argument:
args=()
args+=("A")
args+=("1 2")
args+=("B")
. script2.sh "${args[@]}"
这篇关于如何通过从bash脚本到bash脚本空间的说法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!