List expansion in a 'for in' control structure

2005-07-30 Thread Till Halbach

Configuration Information:
Machine: i386
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i386'  
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i386-pc-linux-gnu'  
-DCONF_VENDOR='pc' -DSHELL -DHAVE_CONFIG_H  -I.  -I../bash  
-I../bash/include -I../bash/lib  -g -O2
uname output: Linux pc052 2.4.27-1-386 #1 Fri Sep 3 06:24:46 UTC 2004 i686  
GNU/Linux

Machine Type: i386-pc-linux-gnu

Bash Version: 2.05b
Patch Level: 0
Release Status: release

Description:
The bash manual for the control structure 'for name [ in word ] ; do list  
; done' says:
'The list of words following in is expanded, generating a  list  of  
items.' However, if no files are found, it is set equal to the query  
string.


Repeat-By:
Assume you have a directory with some files beginning with 'a' and none  
beginning with 'z'.

for file in a*.html; do echo $file; done

abbr.html
acronym.html
address.html
a.html
applet.html

for file in z*.html; do echo $file; done

z*.html

Fix:
If no files are found, the list of words (represented by word in the  
command description above) should expand to a zero-element array, but not  
to the word itself.


BR,
Till


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: List expansion in a 'for in' control structure

2005-07-30 Thread Bob Proulx
Till Halbach wrote:
 Bash Version: 2.05b

 The bash manual for the control structure 'for name [ in word ] ; do list  
 ; done' says:
 'The list of words following in is expanded, generating a  list  of  
 items.' However, if no files are found, it is set equal to the query  
 string.

Yes.  That is correct.

You are apparently mixing two behaviors together.  This really has
nothing to do with the for loop.  The for loop is simply looping
through its arguments.  This really has to to do with how the shell
does pathname expansion which is part of the bigger problem of command
line expansion.  In your case above don't think about the for loop,
think about just the filename expansion.  For example you can use
echo to reduce this to a simpler problem.

  echo GLOB

 Assume you have a directory with some files beginning with 'a' and none  
 beginning with 'z'.
 for file in a*.html; do echo $file; done
 abbr.html
 acronym.html
 address.html
 a.html
 applet.html

Yes.  The glob pattern matched the files in the directory.  They are
expanded.  The for loop is passed the result of the expansion.  You can
also repeat this simply with echo.

  echo a*.html

The for loop is then looping through all of the arguments provided to
it.

 for file in z*.html; do echo $file; done
 z*.html

Yes.  You can repeat this simply with echo.

  echo z*.html

The for loop is then looping through all of the arguments provided to
it.  You are reporting this as if you think this must be a bug.  But
this is the correct behavior for a shell to have in this case.

 If no files are found, the list of words (represented by word in the  
 command description above) should expand to a zero-element array, but not  
 to the word itself.

No.  That would not match the expected shell behavior.  The expected
shell behavior is that if the file glob pattern is not expanded by the
shell then the string itself will remain verbatim.

You can get the behavior you are asking for by setting the nullglob
shell option.

  shopt -s nullglob

The bash manual has this documentation:

  nullglob
  If set, bash allows patterns which match no
  files (see Pathname Expansion above) to expand
  to a null string, rather than themselves.

Please see the Pathname Expansion section of the bash manual for more
information.

Bob


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: List expansion in a 'for in' control structure

2005-07-30 Thread Chet Ramey
Till Halbach wrote:

 The bash manual for the control structure 'for name [ in word ] ; do
 list  ; done' says:
 'The list of words following in is expanded, generating a  list  of 
 items.' However, if no files are found, it is set equal to the query 
 string.

If you want filename expansion patterns which match no files to expand
to the null string rather than themselves, set the `nullglob' option
with `shopt -s nullglob'.

The word following `in' is expanded using the normal set of expansions.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
Live...Laugh...Love
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash