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?

  • 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[@]}" – Stéphane Chazelas Jan 16 '15 at 17:12
2

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

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

output

Hello there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.