Skip to main content
added 108 characters in body
Source Link

The code is working as expected:

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    list_arrays "${array_one[@]}" "${array_two[@]}"
}

The arguments passed are not two arrays, but 1 2 3 4 5 one two three four five.

The function list_arrays prints, not the arguments passed but the defined local array_one and local array_two in function main

You can see this by changing your code to

function list_arrays {
    # no variable definition
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    # no arguments passed
    list_arrays
}

Output:

1
2
3
4
5
one
two
three
four
five

The variables local _array_one=$1 and local _array_two=$2 are not really used at all in your code.


When you redefine the variables, as in

function list_arrays {
    local array_one=$1 # overwrites main's array_one
    local array_two=$2 # overwrites main's array_two
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )

    # arguments passed are 1 2 3 4 5 one two three four five
    list_arrays "${array_one[@]}" "${array_two[@]}"

}

now array_one is equal to the first argument, 1, and array_two to the second argument, 2, so the output is

1
2

The code is working as expected:

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    list_arrays "${array_one[@]}" "${array_two[@]}"
}

The arguments passed are not two arrays, but 1 2 3 4 5 one two three four five.

The function list_arrays prints, not the arguments passed but the defined local array_one and local array_two in function main

You can see this by changing your code to

function list_arrays {
    # no variable definition
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    # no arguments passed
    list_arrays
}

Output:

1
2
3
4
5
one
two
three
four
five

When you redefine the variables, as in

function list_arrays {
    local array_one=$1 # overwrites main's array_one
    local array_two=$2 # overwrites main's array_two
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )

    # arguments passed are 1 2 3 4 5 one two three four five
    list_arrays "${array_one[@]}" "${array_two[@]}"

}

now array_one is equal to the first argument, 1, and array_two to the second argument, 2, so the output is

1
2

The code is working as expected:

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    list_arrays "${array_one[@]}" "${array_two[@]}"
}

The arguments passed are not two arrays, but 1 2 3 4 5 one two three four five.

The function list_arrays prints, not the arguments passed but the defined local array_one and local array_two in function main

You can see this by changing your code to

function list_arrays {
    # no variable definition
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    # no arguments passed
    list_arrays
}

Output:

1
2
3
4
5
one
two
three
four
five

The variables local _array_one=$1 and local _array_two=$2 are not really used at all in your code.


When you redefine the variables, as in

function list_arrays {
    local array_one=$1 # overwrites main's array_one
    local array_two=$2 # overwrites main's array_two
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )

    # arguments passed are 1 2 3 4 5 one two three four five
    list_arrays "${array_one[@]}" "${array_two[@]}"

}

now array_one is equal to the first argument, 1, and array_two to the second argument, 2, so the output is

1
2
Source Link

The code is working as expected:

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    list_arrays "${array_one[@]}" "${array_two[@]}"
}

The arguments passed are not two arrays, but 1 2 3 4 5 one two three four five.

The function list_arrays prints, not the arguments passed but the defined local array_one and local array_two in function main

You can see this by changing your code to

function list_arrays {
    # no variable definition
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )
    # no arguments passed
    list_arrays
}

Output:

1
2
3
4
5
one
two
three
four
five

When you redefine the variables, as in

function list_arrays {
    local array_one=$1 # overwrites main's array_one
    local array_two=$2 # overwrites main's array_two
    for ip in "${array_one[@]}"; do
        echo "$ip"
    done
    for node in "${array_two[@]}"; do
        echo "$node"
    done
}

function main {
    local array_one=( 1 2 3 4 5 )
    local array_two=( one two three four five )

    # arguments passed are 1 2 3 4 5 one two three four five
    list_arrays "${array_one[@]}" "${array_two[@]}"

}

now array_one is equal to the first argument, 1, and array_two to the second argument, 2, so the output is

1
2