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

时间:2022-06-12 16:48:53

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

4 个解决方案

#1


18  

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

Bash数组不是“第一类值” - 你不能像一个“东西”那样传递它们。

Assuming test.sh is a bash script, I would do

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

#!/bin/bash
arg1=$1; 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

#2


10  

Have your script arrArg.sh like this:

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

#!/bin/bash

arg1="$1"
arg2=("${!2}")
arg3="$3"
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 . ./script syntax. 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?答:因为bash没有将数组变量导出到子进程,如bash作者本人所述

#3


0  

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

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

array.sh

array.sh

array=(a b c)

test.sh

test.sh

source $2
...

Run the test.sh script:

运行test.sh脚本:

./test.sh argument1 array.sh argument3

#4


-3  

If this is your command:

如果这是你的命令:

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

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

您可以将数组读入test.sh,如下所示:

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

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

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

#1


18  

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

Bash数组不是“第一类值” - 你不能像一个“东西”那样传递它们。

Assuming test.sh is a bash script, I would do

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

#!/bin/bash
arg1=$1; 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

#2


10  

Have your script arrArg.sh like this:

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

#!/bin/bash

arg1="$1"
arg2=("${!2}")
arg3="$3"
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 . ./script syntax. 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?答:因为bash没有将数组变量导出到子进程,如bash作者本人所述

#3


0  

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

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

array.sh

array.sh

array=(a b c)

test.sh

test.sh

source $2
...

Run the test.sh script:

运行test.sh脚本:

./test.sh argument1 array.sh argument3

#4


-3  

If this is your command:

如果这是你的命令:

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

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

您可以将数组读入test.sh,如下所示:

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

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

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