1) Please use
[code][/code] tags around your code, to preserve formatting and to improve readability.
2) When setting a variable, you can't have any spaces around the equals sign.
3)
When you quote a variable/parameter, word-splitting does not occur and the contents are treated as a single item. Remove the quotes when you
want the string to be split into individual "words", which in this case will place them into separate array elements (word splitting being defined by the IFS shell variable, space/tab/newline by default).
Exception: quoting
"${array[@]}" will expand to each individual array element, with those elements being treated as if they were quoted separately. So when you call your function, you're not getting a $2, you're getting a "$2" "$3" "$4", et cetera.
A better way to handle your function then would be like this:
Code:
foo() {
name=$1
shift
array=( $@ )
}
foo "$NAME" "${arrayP[@]}"
You might consider declaring your function variables to be local as well.
4)
$(..) is recommended over `..`, and seq isn't usually necessary when you have
brace expansion, although it's not easy to use variables with them.
A better way to iterate through the array elements though is to use this:
Code:
for i in ${!array[@]}; do
echo "${array[i]}"
done
...which expands to a list of all the existing index numbers in the array.
5) Finally, why not simply set your arrayP like this?
Code:
arrayP=( TEST TESTB TESTC )
You can even use brace expansion here, especially if you have more items in the sequence.
Code:
arrayP=( TEST TEST{B..C} )