I'd like to pass an array as argument to a function and add a new element to the array. Then pass the array to another function and print its contents. (All this in Bash.)
syntax error near unexpected token `"$2"'
` $1+=("$2")'
This is all I get, probably because when assigning a value to a variable $ can't be used. I don't know how to solve this problem. Can you help me?
Here is my approach:
#/bin/bash
add_element()
{
$1+=("$2")
}
print_array()
{
for i in "${$1[@]}"
do
echo "$i"
done
}
declare -a ARRAY
add_element ARRAY "a"
add_element ARRAY "b"
add_element ARRAY "1,2"
add_element ARRAY "d"
print_array ARRAY