Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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

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

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

output

Hello there
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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