How to skip the for loop when there are no matching files?

You can stop this behaviour by setting nullglob:

shopt -s nullglob

From the linked page:

nullglob is a Bash shell option which modifies [[glob]] expansion
such that patterns that match no files expand to zero arguments,
rather than to themselves.

You can remove this setting with -u (unset, whereas s is for set):

shopt -u nullglob

Test

$ touch foo1 foo2 foo3
$ for file in foo*; do echo "$file"; done
foo1
foo2
foo3
$ rm foo*

Let’s see:

$ for file in foo*; do echo "$file"; done
foo*

Setting nullglob:

$ shopt -s nullglob
$ for file in foo*; do echo "$file"; done
$

And then we disable the behaviour:

$ shopt -u nullglob
$ for file in foo*; do echo "$file"; done
foo*

Leave a Comment