bug-bash
[Top][All Lists]
Advanced

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

Re: docs incorrectly mention pattern matching works like pathname expans


From: Stormy
Subject: Re: docs incorrectly mention pattern matching works like pathname expansion
Date: Fri, 16 Mar 2018 13:49:55 +0000 (UTC)

Greg,
Thanks for all the detailed explanation, i think all your examples relate to 
existing files.

Maybe it was not clear from the start, so let me say that again.  In the input 
to my script, both the pattern and the filenames do not relate to actual files 
on any existing filesystem -- they are just 'strings', hence something like 
/data/dir1/* will not expand to anything, b/c that filesystem does not exist 
(heck, it may exist, but that is not a given and should not impact the 
decision/execution).. same for pathnames.
the goal was, given two sets of inputs one patterns, like: /data/dir1/*, 
/data/dir2/*/?/[0-9], etc. and another fixed full file names, i.e: 
/data/dir1/test, /data/dir2/test//test2   one has to print no/match for all 
these combinations; if bash had 'fnmatch' "builtin" it would be a one liner 
like [[ $a =/ $b ]] :)  I just invented an imaginary comparison operator :) :)

I do know how globbing works in "echo *", maybe the language I use is not as 
precise as you guys , excuse me for that, I'm just a simple end user of bash 
that wanted to report the original 'doc issue/bug' with regards to 
pattern/matching...

Cheers.
Stormy.
 

    On Friday, March 16, 2018, 2:24:34 PM GMT+2, Greg Wooledge 
<wooledg@eeg.ccf.org> wrote:  
 
 On Fri, Mar 16, 2018 at 10:04:07AM +0000, Stormy wrote:
> Thanks for the confirmation regarding the need to write code. and also about 
> 'cd' 'ls', yeah, it's what I sort of mean, bash does when passing args to 
> them, but there is no other way to 'access' this type of parsing..

You are still quite confused.  Globbing (filename expansion) is done by
bash when a glob is evaluated in several different contexts.  There are
ways to use this.  Real ways.  This is not just esoteric bullshit.

Context #1 is when the glob is part of an argument list.  I gave this
example previously in this thread:

  echo *

Do you not understand how this works?  Bash expands the glob (*) into
a list of filename argument-words.  Each argument is passed to echo
as part of echo's argv[] array.  Then, echo prints each of them, because
that's what echo does.

Passing an argument list of unbounded size to a command may run into
system-specific length limits.  See
<https://www.in-ulm.de/~mascheck/various/argmax/> for details.

Context #2 is when the glob is part of an array assignment, like this:

  files=(*)

In this case, the glob is expanded into a list of filenames, which are
then used as the argument-words to stuff into the array.  The result
is an array containing all of the filenames that matched the glob, one
filename per array element, one array element per filename.

Since this is not a command's argv[] there is no limit on the length,
other than however much memory bash can allocate.

Context #3 is when the glob is one of the words following
"for varname in".

  for f in *

The glob is expanded to a list of filenames which is stored internally
and used as the iteration list of the for loop.  You don't have direct
access to the complete list of filenames at any point (use an array if
that's what you want), but you do get access to one filename at a time
as the for loop iterates.  Each filename becomes the content of the
variable named "f", one by one.

Again, there is no limit on the length of this internal filename list,
other than the amount of memory bash can allocate.
  

reply via email to

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