如何将数组参数传递给 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17232526/
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:51:50  来源:igfitidea点击:

How to pass an array argument to the Bash script

arraysbasharguments

提问by zhihong

It is surprising me that I do not find the answer after 1 hour search for this. I would like to pass an array to my script like this:

令我惊讶的是,我在搜索了 1 小时后没有找到答案。我想像这样将数组传递给我的脚本:

test.sh argument1 array argument2

I DO NOT want to put this in another bash script like following:

我不想把它放在另一个 bash 脚本中,如下所示:

array=(a b c)
for i in "${array[@]}"
do
  test.sh argument1 $i argument2
done

回答by glenn Hymanman

Bash arrays are not "first class values" -- you can't pass them around like one "thing".

Bash 数组不是“一等值”——你不能像一个“东西”一样传递它们。

Assuming test.shis a bash script, I would do

假设test.sh是一个 bash 脚本,我会这样做

#!/bin/bash
arg1=; shift
array=( "$@" )
last_idx=$(( ${#array[@]} - 1 ))
arg2=${array[$last_idx]}
unset array[$last_idx]

echo "arg1=$arg1"
echo "arg2=$arg2"
echo "array contains:"
printf "%s\n" "${array[@]}"

And invoke it like

并像这样调用它

test.sh argument1 "${array[@]}" argument2

回答by anubhava

Have your script arrArg.shlike this:

让你的脚本arrArg.sh是这样的:

#!/bin/bash

arg1=""
arg2=("${!2}")
arg3=""
arg4=("${!4}")

echo "arg1=$arg1"
echo "arg2 array=${arg2[@]}"
echo "arg2 #elem=${#arg2[@]}"
echo "arg3=$arg3"
echo "arg4 array=${arg4[@]}"
echo "arg4 #elem=${#arg4[@]}"

Now setup your arrays like this in a shell:

现在在 shell 中像这样设置你的数组:

arr=(ab 'x y' 123)
arr2=(a1 'a a' bb cc 'it is one')

And pass arguments like this:

并传递这样的参数:

. ./arrArg.sh "foo" "arr[@]" "bar" "arr2[@]"

Above script will print:

上面的脚本将打印:

arg1=foo
arg2 array=ab x y 123
arg2 #elem=3
arg3=bar
arg4 array=a1 a a bb cc it is one
arg4 #elem=5

Note:It might appear weird that I am executing script using . ./scriptsyntax. Note that this is for executing commands of the script in the current shell environment.

注意:我使用. ./script语法执行脚本可能看起来很奇怪。请注意,这是为了在当前 shell 环境中执行脚本的命令。

Q.Why current shell environment and why not a sub shell?
A.Because bash doesn't export array variables to child processes as documented here by bash author himself

问:为什么是当前的 shell 环境,为什么不是子 shell?
A.因为 bash 不会将数组变量导出到子进程,正如bash 作者自己在此处记录的那样

回答by Dd H

You can write your array to a file, then source the file in your script. e.g.:

您可以将数组写入文件,然后在脚本中获取该文件。例如:

array.sh

数组文件

array=(a b c)

test.sh

测试文件

source 
...

Run the test.sh script:

运行 test.sh 脚本:

./test.sh argument1 array.sh argument3

回答by Julie Winterburn

If this is your command:

如果这是您的命令:

test.sh argument1 ${array[*]} argument2

You can read the array into test.sh like this:

您可以像这样将数组读入 test.sh:

arg1=
arg2=${2[*]}
arg3=

It will complain at you ("bad substitution"), but will work.

它会向你抱怨(“糟糕的替代”),但会起作用。