8

How can i populate an array in loop? I'd like to do something like that:

declare -A results

results["a"]=1
results["b"]=2

while read data; do
results[$data]=1
done

for i in "${!results[@]}"
do
  echo "key  : $i"
  echo "value: ${results[$i]}"
done

But it seems that I cannot add anything to an array within for loop. Why?

1
  • 2
    What version of bash are you using? Commented Apr 2, 2012 at 23:18

2 Answers 2

26

What you have should work, assuming you have a version of Bash that supports associative arrays to begin with.

If I may take a wild guess . . . are you running something like this:

command_that_outputs_keys \
  | while read data; do
        results[$data]=1
    done

? That is — is your while loop part of a pipeline? If so, then that's the problem. You see, every command in a pipeline receives a copy of the shell's execution environment. So the while loop would be populating a copy of the results array, and when the while loop completes, that copy disappears.

Edited to add: If that is the problem, then as glenn jackman points out in a comment, you can fix it by using process substitution instead:

while read data; do
    results[$data]=1
done < <(command_that_outputs_keys)

That way, although command_that_outputs_keys will receive only a copy of the shell's execution environment (as before), the while loop will have the original, main environment, so it will be able to modify the original array.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 the way to avoid the subshell is to use process substitution: while ... done < <(command here)
1

That seems to be working fine:

$ cat mkt.sh 
declare -A results

results["a"]=1
results["b"]=2

while read data; do
  results[$data]=1
done << EOF
3
4
5
EOF

for i in "${!results[@]}"
do
  echo "key  : $i"
  echo "value: ${results[$i]}"
done

$ ./mkt.sh 
key  : a
value: 1
key  : b
value: 2
key  : 3
value: 1
key  : 4
value: 1
key  : 5
value: 1
$ 

Ubuntu 11.10 here, bash: GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.