1 |
On Sun, Jun 28, 2015 at 10:14 AM, Alexis Ballier <aballier@g.o> wrote: |
2 |
> On Sun, 28 Jun 2015 14:46:01 +0200 |
3 |
> Patrice Clement <monsieurp@g.o> wrote: |
4 |
> |
5 |
>> man 3 glob |
6 |
>> man 3 fnmatch |
7 |
>> man 1 bash -> read the section called "Pattern Matching" under |
8 |
>> "EXPANSION". |
9 |
>> |
10 |
>> in this case, $(ls) would unnecessarily spawn a subshell for listing |
11 |
>> files. You can often get the same result by using wildcard expansion |
12 |
>> (or globbing) like Michal said: |
13 |
>> |
14 |
>> for lang in *; do |
15 |
>> ... |
16 |
>> done |
17 |
>> |
18 |
>> It is often faster. |
19 |
> |
20 |
> |
21 |
> and it avoids aliases: |
22 |
> |
23 |
> /etc $ echo $(ls) |
24 |
> total 1.7M drwxr-xr-x 4 root root 4.0K Aug 29 2014 acpi -rw-r--r-- 1 |
25 |
> root root 44 Oct 6 2014 adjtime drwxr-xr-x 2 root root 4.0K Jun 26 |
26 |
> 19:27 adobe drwxr-xr-x 3 root root 4.0K Mar 22 15:46 ardour3 drwxr-xr-x |
27 |
> 3 root root 4.0K May 8 13:14 ardour4 drwxr-xr-x 2 root root 4.0K Jun 15 |
28 |
> 17:06 at-spi2 drwxr-xr-x 3 root root etc. |
29 |
> |
30 |
> I doubt that is what is wanted |
31 |
> |
32 |
> |
33 |
> |
34 |
> However, beware of empty directories: |
35 |
> |
36 |
> /tmp/toto $ ls |
37 |
> total 0 |
38 |
> /tmp/toto $ for a in *; do echo $a; done |
39 |
> * |
40 |
> |
41 |
|
42 |
Portage doesn't enable shopt expand_aliases to my knowledge, and it |
43 |
wouldn't matter if it did because aliases aren't inherited from parent |
44 |
shells. |
45 |
|
46 |
The reason for a glob aside from performance is pathname |
47 |
expansion being evaluated last, after field splitting and all other |
48 |
expansions. Files with names containing characters in IFS or glob |
49 |
metacharacters will be mangled. `for x in $(anything)' is almost always |
50 |
wrong. |
51 |
|
52 |
A correct loop over files should be close to this (when nullglob is |
53 |
disabled): |
54 |
|
55 |
for x in *; do |
56 |
[[ -e $x ]] || continue |
57 |
... |
58 |
done |