Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
2  
Not entirely related to your question, but don't forget to quote "${f}", to deal with files containing whitespace or globbing characters. – slhck Jul 30 '13 at 21:12
up vote 7 down vote accepted

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:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .