Programming Tips - How can I make my bash glob code work nicely when there are no files
Q. How can I make my bash glob code work nicely when there are no files
A. This code:
for I in *.jpg; do
Works fine unless there are no .jpg files.
In that case it executes the loop once with I set to *.jpg -- not very nice.
Here's an variation that handles zero matches:
for I in $(ls *.jpg 2>/dev/null); do
This works but is slightly slower since it shells out the ls command.