This question already has answers here:
Passing An Array From One Bash Script to Another

(2个答案)


4年前关闭。




我如何将数组作为变量从第一个bash shell脚本传递到第二个脚本。

第一.sh
#!/bin/bash
AR=('foo' 'bar' 'baz' 'bat')
sh second.sh "$AR" # foo
sh second.sh "${AR[@]}" # foo

second.sh
#!/bin/bash
ARR=$1
echo ${ARR[@]}

在这两种情况下,结果均为foo。但是我想要的结果是foo bar baz bat

我在做什么错,该如何解决?

最佳答案

利用

sh second.sh "${AR[@]}"

将数组元素拆分为不同的参数,即
sh second.sh "${A[0]}" "${A[1]}" "${A[2]}" ...

并在second.sh中使用
ARR=("$@")

将命令行参数收集到数组中。

关于arrays - 如何将数组传递给bash shell脚本? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36808024/

10-13 08:41
查看更多