I want to create an associative array in Bash with file names derived from a glob as keys. Here is what I am doing:
declare -A file_hash
for file in *; do
file_hash+=([$file]=1)
done
I am curious to see if there is a more straightforward way of doing the same thing, somewhat like how we initialize a normal array this way:
file_array=(*)
One of the ideas I got from another post on SO is:
file_array=(*)
declare -A file_hash=( $(echo ${file_array[@]} | sed 's/[^ ]*/[&]=&/g') )
Is that the best way?
declare -A filehash; eval $(printf 'filehash+=(["%s"]=1);' *.txt)