1

What's the proper way to do a for loop over a file mask?

E.g. if the mask doesn't expand to any file, then the loop should not run; else, it should run over all the files that it expands to.

The problem with naive approach is that if the * doesn't expand to anything, then the whole loop will run once as if the * part is an actual part of a filename (of a file that doesn't actually exist).

rici
  • 234,347
  • 28
  • 237
  • 341
cnst
  • 25,870
  • 6
  • 90
  • 122

2 Answers2

6

One way to do this is:

for f in abc*; do if [[ -f "$f" ]]; then
  # Do something with "$f"
fi; done

That will also filter directories out of the list, which might or might not be what you want.

If you want to keep directories, use -e instead of -f.

Another way to do it is to set the shell option nullglob (may not be available on all bash versions). With nullglob set, patterns which don't match any filename will result in nothing instead of being unaltered.

shopt -s nullglob
for f in abc*; do
  # do something with $f
done
shopt -u nullglob

(That leaves nullglob set for the duration of the for loop. You can unset it inside the loop instead of waiting for the end, at the smallish cost of executing the shopt -u on every loop.)

rici
  • 234,347
  • 28
  • 237
  • 341
1

Use the nullglob shell option to make a wildcard that doesn't match anything expand into nothing instead of returning the wildcard itself:

shopt -s nullglob
for file in abc*
do
    ...
done
Barmar
  • 741,623
  • 53
  • 500
  • 612