Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:02:33+00:00 2026-06-01T06:02:33+00:00

How can i populate an array in loop? I’d like to do something like

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:02:35+00:00Added an answer on June 1, 2026 at 6:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I populate multiple datagrids in flex with a single datasource that is
I am trying to generate an NSDictonary that can be used to populate a
I have a class SomeClass, which can populate itself from a datarow in it's
How can I get the variable My.Application.Info.Version.ToString to populate in the comments section? Dim
How can I do this in Javascript/jQuery from PHP? I am trying to populate
Is it possible? Developers can use SMS URLs to populate the phone number, but
Can someone point me to an article that shows the dropdownlist being populated from
I got a combobox that is populated from a access database. How can I
Can somebody point me to a resource that explains how to go about having
I use this arrayCollection to populate a Flex 3 Datagrid. I'd also like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.