2

If I have a variable that has a value stored and I want to place that value into an array using the name of the variable, how would I do this?

e.g.:

variable="Hello there"
array[0]=$variable

does not make array[0] equal to "Hello there"

What am I doing wrong?

1
  • Possibly you invoked the split+glob operator with cmd ${array[0]} or cmd ${array[@]}. That should be cmd "${array[0]}" or cmd "${array[@]}", like printf '<%s>\n' "${array[@]}" Jan 16, 2015 at 17:12

1 Answer 1

2

The array assignment is correct, you should check the printing part.

variable="Hello there"
array[0]=$variable
echo "${array[0]}"

output

Hello there

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .