I want to loop over all files matching extension jpg
or txt
. I use:
for file in myDir/*.{jpg,txt}
do
echo "$file"
done
Problem: If the directory contains no jpg file at all, the loop will have one iteration with output myDir/*.jpg
. I thought *
will be replaced by an arbitrary file (and if no file exists it cannot be expanded). How can I avoid the unwanted iteration?
bash
shorthand for generating a set of related strings, and as such expand to the stringmyDir/*.jpg myDir/*.txt
before pattern matching begins. – Noreennorene