help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Glob star pattern does not match files beginning with a


From: Eric Blake
Subject: Re: [Help-bash] Glob star pattern does not match files beginning with a period
Date: Thu, 16 Jul 2015 21:23:32 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1

On 07/16/2015 06:58 PM, Michael Convey wrote:
> On Wed, Jul 15, 2015 at 1:19 PM, Eric Blake <address@hidden> wrote:
> 
>>
>> Both of these three-glob approaches can be used to obtain all file names
>> that are not '.' or '..':
>>
>> ls -d * .[!.]* ..?*
>>
>> or:
>>
>> ls -d * .??* .[!.]
>>
>> but you still have to deal with the fact that unless nullglob  is
>> enabled to eliminate a glob that has no matches, you are then passing
>> unexpanded globs to ls that may result in listing a valid file name
>> twice, or in ls reporting an error about a file not found.
>>
> 
> I understand an error will result, but I don't understand how passing an
> unexpanded glob to ls can ​result in listing a valid filename twice. Please
> explain.

$ cd /tmp
$ mkdir test
$ cd test
$ touch -- '.[!.]'
$ echo * .??* .[!.]
* .[!.] .[!.]
$ ls * .??* .[!.]
ls: cannot access *: No such file or directory
.[!.]  .[!.]
$ shopt -s nullglob
$ ls * .??* .[!.]
.[!.]

Basically, the first glob (*) didn't match, so it is passed verbatim
(without nullglob), but there is no literal file '*', so ls prints an error.

The second glob happens to match the file name (we have a file beginning
with . and at least 3 characters), so it resolves to the file name, and
ls lists '.[!.]'.

The third glob doesn't match, so it is passed verbatim, but it happens
to be the name of a valid file, so ls once again lists '.[!.]'.

And if you DO use nullglob, you then run into the issue you discovered
earlier that if all three globs expand to nothing, then you invoke plain
'ls', which acts as though it had been given the argument '.' (not every
application behaves like that, but for ls, it can confuse you for
figuring out what happened).

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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