on-internet
← Community

Got stuck in dynamically allocating an associative array in bash

Stack Overflow ·

I try to create an associative array in bash like this: #! /bin/bash declare -a arr while read key do arr+=([$key]=1) done < /dev/stdin for i in ${!arr[@]} do echo "key:$i,value:${arr[$i]}" done I have inputed "leopard" and "longtuan" as a key,the output supposed to be like this in my mind: key:leopard,value:1 key:longtuan,value:1 But I only got: key:0,value:11 How can I get through of this problem, Thanks for any help.

Comments

Sign in or sign up to comment and upvote.

No comments yet.