On Sun, Jun 28, 2015 at 10:14 AM, Alexis Ballier <aball...@gentoo.org> wrote:
> On Sun, 28 Jun 2015 14:46:01 +0200
> Patrice Clement <monsie...@gentoo.org> wrote:
>
>> man 3 glob
>> man 3 fnmatch
>> man 1 bash -> read the section called "Pattern Matching" under
>> "EXPANSION".
>>
>> in this case, $(ls) would unnecessarily spawn a subshell for listing
>> files. You can often get the same result by using wildcard expansion
>> (or globbing) like Michal said:
>>
>> for lang in *; do
>>     ...
>> done
>>
>> It is often faster.
>
>
> and it avoids aliases:
>
> /etc $ echo $(ls)
> total 1.7M drwxr-xr-x 4 root root 4.0K Aug 29 2014 acpi -rw-r--r-- 1
> root root 44 Oct 6 2014 adjtime drwxr-xr-x 2 root root 4.0K Jun 26
> 19:27 adobe drwxr-xr-x 3 root root 4.0K Mar 22 15:46 ardour3 drwxr-xr-x
> 3 root root 4.0K May 8 13:14 ardour4 drwxr-xr-x 2 root root 4.0K Jun 15
> 17:06 at-spi2 drwxr-xr-x 3 root root etc.
>
> I doubt that is what is wanted
>
>
>
> However, beware of empty directories:
>
> /tmp/toto $ ls
> total 0
> /tmp/toto $ for a in *; do echo $a; done
> *
>

Portage doesn't enable shopt expand_aliases to my knowledge, and it
wouldn't matter if it did because aliases aren't inherited from parent
shells.

The reason for a glob aside from performance is pathname
expansion being evaluated last, after field splitting and all other
expansions. Files with names containing characters in IFS or glob
metacharacters will be mangled. `for x in $(anything)' is almost always
wrong.

A correct loop over files should be close to this (when nullglob is
disabled):

for x in *; do
    [[ -e $x ]] || continue
    ...
done

Reply via email to