0

I'd like to globber for files in a bash loop, something like this:

for file in filepattern???; do
# some command
done

But, if there are no files that match filepattern???, then this loop executes once with file=filepattern??? exactly, with the question marks in it. I could try this:

for file in $(ls filepattern???); do
# some command
done

That works in a shell, but in a script the failed ls command causes the script to terminate. I'm currently using:

for file in $(for ffile in filepattern???; do echo $ffile; done | grep -v ?); do
# some command
done

That works, but it's really awkward. Any thoughts?

jyoung
  • 61
  • does this answer your question? https://unix.stackexchange.com/questions/239772/bash-iterate-file-list-except-when-empty – user253751 Apr 29 '23 at 00:53

0 Answers0