Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Say I have a folder with three files:

foo1
foo2
bar

1. If I run

list_of_files=$(print foo*)
echo $list_of_files

I get: foo1 foo2

2. If I run

list_of_files=$(print bar*)
echo $list_of_files

I get: bar

3. However, if I run

list_of_files=$(print other*)
echo $list_of_files

I get: zsh: no matches found: other* (the variable $list_of_files is empty though)


Is there a way to ask zsh to not complain if it can't match a glob expansion?

My goal is to use the mechanism above to silently collect a list of files that match a given glob pattern. Any alternative suggestions to do this would be greatly appreciated.

share|improve this question
add comment

4 Answers 4

up vote 9 down vote accepted

Turn on the null_glob option for your pattern with the N glob qualifier.

list_of_files=(*(N))

If you're doing this on all the patterns in a script or function, turn on the null_glob option: setopt null_glob.

This answer has bash and ksh equivalents.

Do not use print or command substitution! That generates a string consisting of the file names with spaces between them, instead of a list of strings. (See What is word splitting? Why is it important in shell programming?)

share|improve this answer
add comment

I think you are looking for the NULL_GLOB option:

   NULL_GLOB (-G)
          If a pattern for filename generation has no matches, delete  the
          pattern  from  the  argument list instead of reporting an error.
          Overrides NOMATCH.
share|improve this answer
add comment

The better way: for a in *(.N); do ... ; done. The N option makes zsh deliver an empty list to for, and for will iterate zero times.

Watch out for ls *.foo(.N); when ls receives an empty argument list, it lists all files instead of none. This is why I don't like NULL_GLOB (or its bash equivalent): It changes all the globs and easily breaks calls to e.g. ls.

share|improve this answer
add comment

Try this way:

list_of_files=$(print other*) 2>/dev/null

It will redirect error output from stderr to /dev/null and it won't display.

share|improve this answer
    
That doesn't work at all. –  pablox Sep 10 '13 at 14:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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