Count elements in bash wildcard expansion?

I have a simple loop in one of my data processing scripts that looks something like this ...

for f in *.dat; do
  process ${f}
done

I'd like to build in a status indicator, so I'd like to be able to count the number of elements in *.dat, but I can't find any clues and Google isn't exactly friendly to this sort of search.


With Bash arrays:

arr=(*.dat)
count=${#arr[@]}

To break this down, ${arr[@]} gives you every element in the array, and # gives you the count in the parameter expansion (actually it'd give you the string length, but for arrays it's the count).

See also, on the Bash Hackers Wiki:

  • Arrays
  • Parameter Expansion