11

I've looked, but have only seen answers to one array being passed in a script.

I want to pass multiple arrays to a bash script that assigns them as individual variables as follows:

./myScript.sh ${array1[@]} ${array2[@]} ${array3[@]}

such that: var1=array1 and var2=array2 and var3=array3

I've tried multiple options, but doing variableName=("$@") combines all arrays together into each variable. I hope to have in my bash script a variable that represents each array.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Rachel
  • 143
  • 2
  • 7
  • Do the arrays contain arbitrary data, or is there a character that you know will not be in the data? For example, if your arrays do not contain something like a comma, I could whip up a solution in a few minutes – Jeffrey Cash Apr 28 '17 at 19:02
  • Bash variables are very simple, you can't have nested structure like this. – Barmar Apr 28 '17 at 19:24
  • @Barmar You can with a little bit of trickery – Jeffrey Cash Apr 28 '17 at 19:26
  • 1
    The parameters passed at the command line are strings. If you want to impose a structure, you'll need to serialize it yourself. (eg, pass in JSON) – William Pursell Apr 28 '17 at 19:27
  • As an aside -- quotes are important. Assuming a default IFS value, `array1=( "hello world" "goodbye world" )` will expand to two values with `"${array1[@]}"`, but to four with unquoted `${array1[@]}`. – Charles Duffy Apr 28 '17 at 20:16
  • 1
    (...and as another aside -- I'd tend to advise *against* using a `.sh` extension. Executable scripts define commands, and commands don't typically have extensions: one doesn't run `ls.elf` or `systriage.py`; extensions can become misleading if a command is rewritten in a different language without updating all callers; moreover, an extension that implies POSIX sh compliance can result in folks invoking a script that requires ksh or bash extension to run correctly with an interpreter that doesn't provide same). – Charles Duffy Apr 28 '17 at 21:31

5 Answers5

14

The shell passes a single argument vector (that is to say, a simple C array of strings) off to a program being run. This is an OS-level limitation: There exists no method to pass structured data between two programs (any two programs, written in any language!) in an argument list, except by encoding that structure in the contents of the members of this array of C strings.


Approach: Length Prefixes

If efficiency is a goal (both in terms of ease-of-parsing and amount of space used out of the ARG_MAX limit on command-line and environment storage), one approach to consider is prefixing each array with an argument describing its length.

By providing length arguments, however, you can indicate which sections of that argument list are supposed to be part of a given array:

./myScript \
  "${#array1[@]}" "${array1[@]}" \
  "${#array2[@]}" "${array2[@]}" \
  "${#array3[@]}" "${array3[@]}"

...then, inside the script, you can use the length arguments to split content back into arrays:

#!/usr/bin/env bash

array1=( "${@:2:$1}" ); shift "$(( $1 + 1 ))"
array2=( "${@:2:$1}" ); shift "$(( $1 + 1 ))"
array3=( "${@:2:$1}" ); shift "$(( $1 + 1 ))"

declare -p array1 array2 array3

If run as ./myScript 3 a b c 2 X Y 1 z, this has the output:

declare -a array1='([0]="a" [1]="b" [2]="c")'
declare -a array2='([0]="X" [1]="Y")'
declare -a array3='([0]="z")'

Approach: Per-Argument Array Name Prefixes

Incidentally, a practice common in the Python world (particularly with users of the argparse library) is to allow an argument to be passed more than once to amend to a given array. In shell, this would look like:

./myScript \
  "${array1[@]/#/--array1=}" \
  "${array2[@]/#/--array2=}" \
  "${array3[@]/#/--array3=}"

and then the code to parse it might look like:

#!/usr/bin/env bash
declare -a args array1 array2 array3
while (( $# )); do
  case $1 in
    --array1=*) array1+=( "${1#*=}" );;
    --array2=*) array2+=( "${1#*=}" );;
    --array3=*) array3+=( "${1#*=}" );;
    *)          args+=( "$1" );;
  esac
  shift
done

Thus, if your original value were array1=( one two three ) array2=( aye bee ) array3=( "hello world" ), the calling convention would be:

./myScript --array1=one --array1=two --array1=three \
           --array2=aye --array2=bee \
           --array3="hello world"

Approach: NUL-Delimited Streams

Another approach is to pass a filename for each array from which a NUL-delimited list of its contents can be read. One chief advantage of this approach is that the size of array contents does not count against ARG_MAX, the OS-enforced command-line length limit. Moreover, with an operating system where such is available, the below does not create real on-disk files but instead creates /dev/fd-style links to FIFOs written to by subshells writing the contents of each array.

./myScript \
  <( (( ${#array1[@]} )) && printf '%s\0' "${array1[@]}") \
  <( (( ${#array2[@]} )) && printf '%s\0' "${array2[@]}") \
  <( (( ${#array3[@]} )) && printf '%s\0' "${array3[@]}")

...and, to read (with bash 4.4 or newer, providing mapfile -d):

#!/usr/bin/env bash
mapfile -d '' array1 <"$1"
mapfile -d '' array2 <"$2"
mapfile -d '' array3 <"$3"

...or, to support older bash releases:

#!/usr/bin/env bash
declare -a array1 array2 array3
while IFS= read -r -d '' entry; do array1+=( "$entry" ); done <"$1"
while IFS= read -r -d '' entry; do array2+=( "$entry" ); done <"$2"
while IFS= read -r -d '' entry; do array3+=( "$entry" ); done <"$3"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Charles Duffy's response works perfectly well, but I would go about it a different way that makes it simpler to initialize var1, var2 and var3 in your script:

./myScript.sh "${#array1[@]} ${#array2[@]} ${#array3[@]}" \
 "${array1[@]}" "${array2[@]}" "${array3[@]}"

Then in myScript.sh

#!/bin/bash
declare -ai lens=($1);
declare -a var1=("${@:2:lens[0]}") var2=("${@:2+lens[0]:lens[1]}") var3=("${@:2+lens[0]+lens[1]:lens[2]}");

Edit: Since Charles has simplified his solution, it is probably a better and more clear solution than mine.

Jeffrey Cash
  • 1,023
  • 6
  • 12
0

Here is a code sample, which shows how to pass 2 arrays to a function. There is nothing more than in previous answers except it provides a full code example.

This is coded in bash 4.4.12, i.e. after bash 4.3 which would require a different coding approach. One array contains the texts to be colorized, and the other array contains the colors to be used for each of the text elements :

function cecho_multitext () {
  # usage : cecho_multitext message_array color_array
  # what it does : Multiple Colored-echo.

    local -n array_msgs=$1
    local -n array_colors=$2
    # printf '1: %q\n' "${array_msgs[@]}"
    # printf '2: %q\n' "${array_colors[@]}"


    local i=0
    local coloredstring=""
    local normalcoloredstring=""

    # check array counts
    # echo "msg size : "${#array_msgs[@]}
    # echo "col size : "${#array_colors[@]}

    [[ "${#array_msgs[@]}" -ne "${#array_colors[@]}" ]] && exit 2

    # build the colored string
    for msg in "${array_msgs[@]}"
    do
      color=${array_colors[$i]}
      coloredstring="$coloredstring $color $msg "
      normalcoloredstring="$normalcoloredstring $msg"
      # echo -e "coloredstring ($i): $coloredstring"
      i=$((i+1))
    done

    # DEBUG
    # echo -e "colored string : $coloredstring"
    # echo -e "normal color string : $normal $normalcoloredstring"

    # use either echo or printf as follows :
    # echo -e "$coloredstring"
    printf '%b\n' "${coloredstring}"

    return
}

Calling the function :

#!/bin/bash
green='\E[32m'
cyan='\E[36m'
white='\E[37m'
normal=$(tput sgr0)
declare -a text=("one" "two" "three" )
declare -a color=("$white" "$green" "$cyan")
cecho_multitext text color

Job done :-)

hornetbzz
  • 9,188
  • 5
  • 36
  • 53
0

I do prefer using base64 to encode and decode arrays like:

encode_array(){
    local array=($@)
    echo -n "${array[@]}" | base64
} 
decode_array(){
    echo -n "$@" | base64 -d
}
some_func(){
    local arr1=($(decode_array $1))
    local arr2=($(decode_array $2))
    local arr3=($(decode_array $3))
    echo arr1 has ${#arr1[@]} items, the second item is ${arr1[2]} 
    echo arr2 has ${#arr2[@]} items, the third item is ${arr2[3]} 
    echo arr3 has ${#arr3[@]} items, the here the contents ${arr3[@]} 
}

a1=(ab cd ef)
a2=(gh ij kl nm)
a3=(op ql)
some_func "$(encode_array "${a1[@]}")" "$(encode_array "${a2[@]}")" "$(encode_array "${a3[@]}")"

The output is

arr1 has 3 items, the second item is cd
arr2 has 4 items, the third item is kl
arr3 has 2 items, the here the contents op ql

Anyway, that will not work with values that have tabs or spaces. If required, we need a more elaborated solution. something like:

encode_array()
{
        for item in "$@";
        do
                echo -n "$item" | base64
        done | paste -s -d , -
}

decode_array()
{
        local IFS=$'\2'

        local -a arr=($(echo "$1" | tr , "\n" |
                while read encoded_array_item;
                do 
                        echo  "$encoded_array_item" | base64 -d;
                        echo "$IFS"
                done))
        echo "${arr[*]}";
}

test_arrays_step1()
{
        local IFS=$'\2'
        local -a arr1=($(decode_array $1))
        local -a arr2=($(decode_array $2))
        local -a arr3=($(decode_array $3))
        unset IFS
        echo arr1 has ${#arr1[@]} items, the second item is ${arr1[1]} 
        echo arr2 has ${#arr2[@]} items, the third item is ${arr2[2]} 
        echo arr3 has ${#arr3[@]} items, the here the contents ${arr3[@]} 
}

test_arrays()
{
        local a1_2="$(echo -en "c\td")";
        local a1=("a b" "$a1_2" "e f");
        local a2=(gh ij kl nm);
        local a3=(op ql );
        a1_size=${#a1[@])};
        resp=$(test_arrays_step1 "$(encode_array "${a1[@]}")" "$(encode_array "${a2[@]}")" "$(encode_array "${a3[@]}")");
        echo -e "$resp" | grep arr1 | grep "arr1 has $a1_size, the second item is $a1_2" || echo but it should have only $a1_size items, with the second item as $a1_2
        echo "$resp"
}

ton
  • 3,827
  • 1
  • 42
  • 40
-1

Based on the answers to this question you could try the following.

Define the arrays as variable on the shell:

array1=(1 2 3)
array2=(3 4 5)
array3=(6 7 8)

Have a script like this:

arg1=("${!1}")
arg2=("${!2}")
arg3=("${!3}")


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

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

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

And call it like this:

. ./test.sh "array1[@]" "array2[@]" "array3[@]"

Note that the script will need to be sourced (. or source) so that it is executed in the current shell environment and not a sub shell.

Community
  • 1
  • 1
saxitoxin
  • 418
  • 3
  • 6
  • 1
    Eh? Basic shell variables *can* be exported to the environment and thus available to subprocesses, but arrays can't. How is this expected to work? – Charles Duffy Apr 28 '17 at 19:35
  • This method would work for a function, but I'm pretty sure @CharlesDuffy is right – Jeffrey Cash Apr 28 '17 at 19:41
  • Charles you are correct. However, as explained in the linked question in the answer by anubhava the trick is in sourcing (. or source) the script so that the script is executed in the current shell environment and not a sub shell. – saxitoxin Apr 28 '17 at 19:50
  • Having a shebang in something that needs to be sourced to work is a bit misleading, then. – Charles Duffy Apr 28 '17 at 20:00
  • @CharlesDuffy yes, once again you are correct. I am going to edit the answer for clarity and remove it. – saxitoxin Apr 28 '17 at 20:03