Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 5 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.