how to handle bash * matching when there are no matches?
Put this magic incantation before the for
statement:
shopt -s nullglob
(documentation)
The nullglob option (@kfmfe04's answer) is best if you are using bash (not a brand-X shell), and don't have to worry about nullglob changing/breaking anything else. Otherwise, you can use this (slightly messier) option:
for txt in *.txt
do
[ -e "$txt" ] || continue
echo "loading data from $txt"
done
This silently skips files that don't exist (mainly "*.txt" if there were no matches, but possibly also files that were deleted between when the for
generated the list and when the loop got to them...)