for loop glob mishaps

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

Stepping a bit sideways, using find might also be useful here if the command you are running for the files is simple enough to drop on one line. Find can take several paths to look in, and will happily find any files in subdirectories too.

$ find foo/ bar/ -name "*.jpg" -exec echo {} \;
foo/ccc.jpg
foo/bbb.jpg
bar/aaa.jpg

(The command executed is given after the -exec flag, ending with the ;. {} gets replaced by the name of the current file.)


add this line to the start of your script

shopt -s nullglob

Tags:

Bash

Wildcards