Skip to main content
edited body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ declaretypeset -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.


As pointed out in the comments below, associative arrays were added in bash version 4. See here for a Linux journal article on the subject.

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ declare -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.


As pointed out in the comments below, associative arrays were added in bash version 4. See here for a Linux journal article on the subject.

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ typeset -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.


As pointed out in the comments below, associative arrays were added in bash version 4. See here for a Linux journal article on the subject.

added 225 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ declare -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.


As pointed out in the comments below, associative arrays were added in bash version 4. See here for a Linux journal article on the subject.

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ declare -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ declare -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.


As pointed out in the comments below, associative arrays were added in bash version 4. See here for a Linux journal article on the subject.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I'm not sure what you mean by store, but you can iterate over the keys using the ${!array[@]} syntax:

$ declare -A foo=([key1]=bar [key2]=baz);
$ echo "${!foo[@]}" 
key2 key1

So, to iterate:

$ for key in "${!foo[@]}"; do echo "$key : ${foo[$key]}"; done
key2 : baz
key1 : bar

I found a nice, short tutorial on this here.