Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: globbing in a for loop may prematurely exit script when sourcing a zsh script

On Wed, 31 Jul 2013 01:13:47 -0700
Chris Hiestand <chiestand@xxxxxxxx> wrote:
> I am new to zsh and this is what I'm trying to do within /etc/zsh/zshenv:
> > if [[ -d "/custom/zsh.d" ]]; then
> >   for f in /custom/zsh.d/*.zsh; do
> >     source $f
> >   done 2> /dev/null
> > fi
> > 
> > #Do more stuff below...
> What happens in zsh 4.3.17 is that if /custom/zsh.d/*.zsh expands to
> no files, zsh acts as if it hits err_return and "Do more stuff" does
> not get executed.

Historically, the shell has certainly been quite aggressive at treating
glob failures as hard errors.  There are various options you can set to
select how the shell treats glob failures.

The right thing to do here is tell the shell to expand the pattern to an
empty string if there are no matches.  You can do this without changing
any options by appending (N) to the pattern, i.e.

  for f in /custom/zsh.d/*.zsh(N); do

This behaves as if the NULL_GLOB option was set for that pattern.  In
zsh (but not necessarily other shells) a "for" loop over an empty
expansion does nothing.


Messages sorted by: Reverse Date, Date, Thread, Author