Bash – How to Avoid For Loop Glob Mishaps?

bashwildcards

I am trying to set up a script that will loop over a set of directories, and do one thing when it finds .jpg files, and another when it finds .nef files. The problem is, if a directory does not have .jpg files for example (or .nef) then the glob entry is no longer an expanded glob, but just a string. For example:

my_dir="pictures/"
ext="JPG"
for f in "$my_dir"*."$ext"; do
    echo $f
done

if the my_dir folder has .JPG files in it, then they will be echoed correctly on the command line.

pictures/one.JPG
pictures/two.JPG

However, if my_dir has no .JPG files, then the loop will enter for one iteration and echo:

pictures/*.JPG

how do I construct this so that if the glob has no matches, it does not enter the for loop?

Best Answer

This is normal and default behavior: If globbing fails to match any files/directories, the original globbing character will be preserved.

If you want to get back an empty result instead, you can set the nullglob option in your script as follows:

$ shopt -s nullglob
$ for f in "$my_dir"*."$ext"; do echo $f; done
$

You can disable it afterwards with:

$ shopt -u nullglob