How to create an array from the keys of an associative array?

Viewed 38

I have an associative array:

declare -A hash

hash[one]=1
hash[two]=2
hash[third item]=3

How can I create an indexed array from the keys of this associative array?

1 Answers

To make an array with the keys from an associative array:

arr=("${!hash[@]}")

To make an array with the values:

arr=("${hash[@]}")

Note that you need the double quotes to prevent the shell from word splitting.

Related