Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Array by index in bash doesn't work correctly if array is from a sourced file

Tags:

In bash when I access an array by index I get strange behavior if the array is a variable that was imported in the source of another bash script. What causes this behavior? How can it be fixed so an array that is sourced from another bash script behaves the same way as an array defined from within the running script?

${numbers[0]} evals to "one two three" and not "one" as it should.The full test I've tried to demonstrate this behavior is shown below:

Source of test.sh :

#!/bin/bash

function test {

    echo "Length of array:"
    echo ${#numbers[@]}

    echo "Directly accessing array by index:"
    echo ${numbers[0]}
    echo ${numbers[1]}
    echo ${numbers[2]}

    echo "Accessing array by for in loop:"
    for number in ${numbers[@]}
    do
        echo $number
    done

    echo "Accessing array by for loop with counter:"
    for (( i = 0 ; i < ${#numbers[@]} ; i=$i+1 ));
    do
        echo $i
        echo ${numbers[${i}]}
    done
}

numbers=(one two three)
echo "Start test with array from within file:"
test 

source numbers.sh
numbers=${sourced_numbers[@]}
echo -e "\nStart test with array from source file:"
test

Source of number.sh :

#!/bin/bash
#Numbers

sourced_numbers=(one two three)

Output of test.sh :

Start test with array from within file:
Length of array:
3
Directly accessing array by index:
one
two
three
Accessing array by for in loop:
one
two
three
Accessing array by for loop with counter:
0
one
1
two
2
three

Start test with array from source file:
Length of array:
3
Directly accessing array by index:
one two three
two
three
Accessing array by for in loop:
one
two
three
two
three
Accessing array by for loop with counter:
0
one two three
1
two
2
three
like image 989
Rebecca Meritz Avatar asked May 02 '12 13:05

Rebecca Meritz


People also ask

How do I index an array in bash?

Similar to other programming languages, Bash array elements can be accessed using index number starts from 0 then 1,2,3…n. This will work with the associative array which index numbers are numeric. To print all elements of an Array using @ or * instead of the specific index number.

Can you have an array of arrays in bash?

Still, multidimensional arrays aren't supported by bash, and we can't get array components that are also arrays. Fortunately, multidimensional arrays can be simulated. This article will provide some illustrations of the simulation of an array of arrays in a bash script.

How do I echo an array in bash?

How to Echo a Bash Array? To echo an array, use the format echo ${Array[0]}. Array is your array name, and 0 is the index or the key if you are echoing an associative array. You can also use @ or * symbols instead of an index to print the entire array.


1 Answers

The problem has nothing to do with sourcing; it's happening because the assignment numbers=${sourced_numbers[@]} doesn't do what you think it does. It converts the array (sourced_numbers) into a simple string, and stores that in the first element of numbers (leaving "two" "three" in the next two elements). To copy it as an array, use numbers=("${sourced_numbers[@]}") instead.

BTW, for number in ${numbers[@]} is the wrong way to loop through an array's elements, because it'll break on whitespace in the elements (in this case, the array contains "one two three" "two" "three", but the loop runs for "one", "two", "three", "two", "three"). Use for number in "${numbers[@]}" instead. Actually, it's good to get in the habit of double-quoting pretty much all variable substitutions (e.g. echo "${numbers[${i}]}"), as this is not the only place where leaving them unquoted could cause trouble.

like image 171
Gordon Davisson Avatar answered Sep 28 '22 08:09

Gordon Davisson