There is my script (simplified):

cd /etc; for f in *.{conf,cfg,foo}; do echo $f; done

It outputs something like


(just an example)

Just for now I using something like

for f in *.{conf,cfg,foo}; do test -f $f && echo $f; done

because of *.foo (no files matched to the mask)

Is there any more convenient solution (e.g. do not list *.ext if no such files found)?

In other words: I want to avoid *.foo line in the output when no files matched to the *.foo mask.


You can use:

shopt -s nullglob

Disable it again using:

shopt -u nullglob

If you're writing a shell function that needs to unconditionally enable or disable a shopt setting temporarily and then revert back to whatever the previous state was, you could do:

oldshopt=$(shopt -p)
shopt -s nullglob
<do stuff...>
eval "$oldshopt"

There is also the opposite option, failglob, which causes an error message whenever a glob expansion fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.