1 |
Hi David, |
2 |
on Thursday, 2005-12-29 at 13:53:17, you wrote: |
3 |
> > $(ls *.jpg) |
4 |
> |
5 |
> ick! |
6 |
> |
7 |
> (incidentally, http://www.ruhr.de/home/smallo/award.html#ls) |
8 |
|
9 |
Well, it's bad in two ways, and even the example on the above webpage is |
10 |
wrong. For one thing, "ls" is useless here. For another, it will break |
11 |
on spaces in filenames, unlike shell globbing: |
12 |
| $ touch "foo bar.jpg" |
13 |
| $ for f in *.jpg; do echo $f; done |
14 |
| foo bar.jpg |
15 |
| $ for f in `ls *.jpg`; do echo $f; done |
16 |
| foo |
17 |
| bar.jpg |
18 |
| $ for f in `ls *.jpg`; do echo "$f"; done |
19 |
| foo |
20 |
| bar.jpg |
21 |
The bottommost try shows that the comment "newbies will often forget the |
22 |
quotes, too" is wrong -- it won't work either way. If you have to use |
23 |
a program that outputs a filename per line like ls, use a read loop: |
24 |
| $ ls *.jpg | while read f; do echo "$f"; done |
25 |
| foo bar.jpg |
26 |
The quotes are useless for "echo" here, but for other commands you'll |
27 |
usually need them to keep the command form taking filenames with sapaces |
28 |
as separate arguments. |
29 |
|
30 |
cheers! |
31 |
Matthias |
32 |
|
33 |
-- |
34 |
I prefer encrypted and signed messages. KeyID: FAC37665 |
35 |
Fingerprint: 8C16 3F0A A6FC DF0D 19B0 8DEF 48D9 1700 FAC3 7665 |