0

I am using arrays in bash and one particular array is behaving unusually.

I am using a function and calling an external script which returns a value to be appended to an array as follows:

function get_unit_coverage() {

 for sub_unit in "$@"
 do
     extracted_value=$( ./external_script.sh $file $sub_unit )
     my_array+=$extracted_value
 done
}

I pass this function an array and expect the array to be appended each iteration.

However the retun of:

echo "${my_Array[0]}"

is

52.5500%66.6400%16.4300%47.8800%40.6600%45.6800%43.3400%74.5100%87.4600%45.6300%65.6100%58.0900%%47.5800%5.9500%7.6500%1.8000%

The external_script.sh simply echoes these values, is this a potential issue?

  • 1
    Take care on the capitalization also (array vs Array) – Jeff Schaller Mar 7 at 11:45
6

To append new elements to an array:

array+=( new elements here )

In your case:

my_array+=( "$extracted_value" )

When you do

array+=$variable

you are appending to the first element of the array. It is the same as

array[0]+=$variable

Also note that in

extracted_value=$( ./external_script.sh $file $sub_unit )

the values $file and $sub_unit will be split on whitespace and undergo filename globbing. To prevent this, use "$file" and "$sub_unit" instead (i.e. double-quote the variable expansions).

Likewise, saying

my_array+=( $extracted_value )

would split the value of $extracted_value into multiple words, and each word would undergo filename globbing to generate new element in the array. That would be better written (as already mentioned),

my_array+=( "$extracted_value" )

This is general advice and there's no reason to not do this regardless of whether you know your values are already single words containing no globbing characters.

  • Is there a formal term for why I was implicitly only appending to the first index? In Python this would work fine, I don't see why the right hand side would make all the difference when the left hand side and operator is constant. – cc6g11 Mar 7 at 14:20
  • @cc6g11 What you want to use is called a compound assignment in the bash manual. That's the name of variable=( ... ) and variable+=( ... ) and is specifically for assigning to or adding to an array. Both variable="..." and variable+="..." are ordinary variable assignments. When an array name is used as just arrayname in an ordinary assignment, you modify its first element. The same goes for using it like echo "$arrayname" instead of echo "${arrayname[@]}" (i.e. only the first element is printed). – Kusalananda Mar 7 at 14:23
0

You added to a string and read it as an array (as Kusalananda explained well above). But your question shows how a variable can be treated as an array. In fact a simple variable can easily become an array.

$ a+=1
$ a+=2
$ echo "$a"
12
$ echo "${a[0]}"
12
$ a+=(3)
$ echo "$a"
12
$ echo "${a[0]}"
12
$ echo "${a[1]}"
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.