help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] A 'for' loop through an empty directory


From: Greg Wooledge
Subject: Re: [Help-bash] A 'for' loop through an empty directory
Date: Mon, 27 Oct 2014 08:09:27 -0400
User-agent: Mutt/1.4.2.3i

On Mon, Oct 27, 2014 at 10:18:07AM +0100, Christophe Lohr wrote:
> Let's try:
>  $ for i in /foobar/* ; do echo $i; done
> /foobar/*
> 
> Why do I get the string '/foobar/*' and not nothing?

There is a bash option, "shopt -s nullglob", which will cause the
script to act as you expect.

The reason it behaves this way is because unmatched globs are not
expanded.  This allows a command like "ls *.txzt" to produce sane
error messages ("ls: *.txzt: No such file or directory") instead
of giving you the entire directory.

For this reason, it's not recommended to use nullglob in interactive
shells.  It's OK in scripts, though, as long as you understand what
it does.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]