0

I am trying to iterate over the Bash Associate Array but it was printing only Key value pair which is wrong. How can I iterate and print correct key-value pairs.

  #!/bin/bash
  declare -a array=([foo]=bar [baz]=quux [corge]=gult )
  for i in "${!array[@]}"
  do
    echo "key :" $i
    echo "value:" ${array[$i]}
  done

Output: key : 0 value: gult

Raghu Ram
  • 127
  • 1
  • 13
  • Use `declare -A`, not `declare -a`. When you use `declare -a` you're creating a numerically-indexed array, not an associative one. – Charles Duffy Apr 07 '21 at 19:56
  • Because your array is numerically-indexed, when you give it a string as a key, the shell tries to convert that string to a number by looking up any variable by that name that exists in the hopes that it'll have a numeric value. – Charles Duffy Apr 07 '21 at 19:57
  • ...so, my first instinct is to close this as caused-by-typo. Is there a new/different question, other than the `declare -a` vs `declare -A` distinction? – Charles Duffy Apr 07 '21 at 19:57
  • @CharlesDuffy: I tried as you said it was printing like Ram-M001:Desktop Mahesh$ ./Iterate.sh ./Iterate.sh: line 2: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] key : 0 value: gult ' – Raghu Ram Apr 07 '21 at 19:58
  • ...and what happened? If there _was_ no `-A` option, that means your `/bin/bash` is too old to have associative arrays. That's the case on MacOS, f/e, where `/bin/bash` is a 3.x release. They're simply not a supported language feature in that case, as the feature as a whole was introduced in bash 4.0. – Charles Duffy Apr 07 '21 at 19:58
  • If you installed a newer version of bash with MacPorts (or Nix or Homebrew or so forth), change from `#!/bin/bash` to `#!/usr/bin/env bash` to traverse your PATH to find the version of bash to use as the interpreter for running the script. – Charles Duffy Apr 07 '21 at 20:01
  • BTW, you can put `echo "$BASH_VERSION"` in your script to log the version of the interpreter it's run with. – Charles Duffy Apr 07 '21 at 20:02
  • As another aside, `echo "value:" ${array[$i]}` is better written as `echo "value: ${array[$i]}"`, with the expansion included inside the quotes. There isn't a compelling reason to have constant strings quoted, but if you don't quote your variable expansions, [surprising things happen](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Charles Duffy Apr 07 '21 at 20:03
  • (I don't know why every time someone sees an error from `declare -A` they think that means they need to use `declare -a` instead, instead of investigating _why_ `declare -A` failed; they're two different things, but it seems to be a very common assumption -- you aren't the first person to make the same error). – Charles Duffy Apr 07 '21 at 20:04
  • @CharlesDuffy Thanks for finding out. My Bash version is 3.2.57(1)-release. Now I am getting below error message: `#!/usr/bin/env bash echo "$BASH_VERSION" declare -A array=([foo]=bar [baz]=quux [corge]=gult ) for i in "${!array[@]}" do echo "key :" $i "value: ${array[$i]}" done ` Result: 3.2.57(1)-release ./Iterate.sh: line 3: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] key : 0 value: gult – Raghu Ram Apr 07 '21 at 20:09
  • 1
    This is as described above. Bash 3 is too old to have associative arrays. You cannot use associative arrays on bash 3. You must have bash 4 or newer to have associative arrays. – Charles Duffy Apr 07 '21 at 20:10
  • MacOS does not have bash 4 because Apple refuses to comply with the license. If you want bash 4 or MacOS, you need to install it yourself, and make the shebang in your script point to the copy your installed. – Charles Duffy Apr 07 '21 at 20:10
  • BTW, this is described in the answers to the linked duplicate question at https://stackoverflow.com/questions/6047648/associative-arrays-error-declare-a-invalid-option – Charles Duffy Apr 07 '21 at 20:12
  • ...and as another aside, I mentioned that bash 3 was too old back in one of the first comments on this question, at https://stackoverflow.com/questions/66993281/iterating-over-bash-associative-array?noredirect=1#comment118419006_66993281 – Charles Duffy Apr 07 '21 at 20:13

0 Answers0