codeforester
codeforester

Reputation: 43079

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

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?

Upvotes: 2

Views: 52

Answers (1)

codeforester
codeforester

Reputation: 43079

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.

Upvotes: 3

Related Questions