For cycles
for variable [in word]; do
command-list
done
Example: Counting files
for i in *.jpg; do
j=`expr $j + 1`
done
echo $j
Problems:
- expr is executed in a subshell --> expensive
- if there is no jpg-file, the string "*.jpg" is counted
A working solution might be
for i in *.jpg; do
test -f "$i" && echo
done | wc -l
- Note the quotes around the argument: $i could contain blanks, newlines etc.